Question regarding template instantiation of tempory type

Arthur Tse
Mon Nov 15 13:18:00 GMT 2004


I encounter a problem that I'm not sure if it is a feature or a bug:

Consider the function

template<T> void f( T& ) {}

If I write

const int x = 3;

GCC compiles, corrently deducing " T = const int " .

But, if I write


I get
error: invalid initialization of non-const reference of type 'int&' from 
a temporary of type 'int'

GCC cannot deduce " T = const int " from a temporary type, which seems 
unreasonable to me. While I can add a const version to fix that problem:

template<T> void f( const T& ) {}

This is problematic when I want to have, say, n arguments, for this 
would require 2^n versions of a template function -- and this is what I 
am trying to do.

Thanks a lot.

Arthur Tse

More information about the Gcc-help mailing list