Template deduction and null pointer

K

Kaba

Have a look at the following code:

template <typename T>
struct identity
{
typedef T type;
};

template <typename T>
void f(T t, typename identity<T>::type* p)
{
}

int main()
{
int a = 0;
f(a, 0);

return 0;
}

This compiles fine on Comeau C++, and Visual Studio 2008. However, GCC
4.4.5 gives an error:

"error: no matching function for call to ?f(int&, int)?"

Standard or not?
 
K

Kaba

Kaba said:
This compiles fine on Comeau C++, and Visual Studio 2008. However, GCC
4.4.5 gives an error:

"error: no matching function for call to ?f(int&, int)?"

Standard or not?

It is a bug. The bug was reported already in 2005, and acknowledged as
such, but for some reason it has not been fixed.
 
J

Johannes Schaub (litb)

Kaba said:
Have a look at the following code:

template <typename T>
struct identity
{
typedef T type;
};

template <typename T>
void f(T t, typename identity<T>::type* p)
{
}

int main()
{
int a = 0;
f(a, 0);

return 0;
}

This compiles fine on Comeau C++, and Visual Studio 2008. However, GCC
4.4.5 gives an error:

"error: no matching function for call to ?f(int&, int)?"

Standard or not?

The Standard is not exceedingly clear on this. There are two group of
compilers: Those that during template argument deduction ignore the non-
deduced context, and those that after arguments were deduced and substituted
take non-deduced contexts (that then are resolved) and compare them against
the argument in the function call again. Comeau/VC++ fall into the first
camp, while GCC and (apparently) clang fall into the second camp.

The issue isn't as easy as saying "non-deduced contexts are ignored, so why
should the above code be ill-formed?" (which I thought before). The
deduction rules also apply to deduction for class template partial
specializations, and in that case non-deduced contexts must *not* be
ignored.

An issue was instantiated at http://www.open-
std.org/jtc1/sc22/wg21/docs/cwg_active.html#1184 for dealing with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top