This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Question regarding template instantiation of tempory type


Hello,

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;
f(x);

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

But, if I write

f(3);

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]