Instantiating template functions in a conditional expression

Hello,

I want to select an instantiation of a template function

using a conditional expression, then call it.

The following works on other compilers, but

fails to link with CC:

sun02% cat simp1.cpp

template<class T> void foo(){};

int main()

{

bool thing=true;

(thing ? foo<int> : foo<float>)();

}

sun02% CC simp1.cpp

Undefinedfirst referenced

symbolin file

void foo<__type_0>()simp1.o

ld: fatal: Symbol referencing errors. No output written to a.out

If I take the templates out of the conditional all is well:

sun02% cat simp2.cpp

template<class T> void foo(){};

void(*f1)() = foo<int>;

void(*f2)() = foo<float>;

int main()

{

bool thing=true;

(thing ? f1 : f2)();

}

sun02% CC simp2.cpp

sun02%

Version is

sun02% CC -V

CC: Sun C++ 5.8 Patch 121017-02 2006/04/19

Can I report this one as a bug?

Thanks

-- Steve

[1061 byte] By [sjgilbertz] at [2007-11-26 10:32:21]
# 1
I'm not sure that the expression (thing ? foo<int> : foo<float>)();is valid. This will work, and it will call the funciton you want:( thing ? foo<int>() : foo<float>() );
clamage45 at 2007-7-7 2:40:08 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 2

FWIW, I believe it is valid, and most compilers accept it. Both foo<int> and foo<float> have the same function type so the type of the conditional expression after function-to-pointer conversion is a pointer to function of that type (i.e., (void(*)(void)). Applying the function call operator to a function (pointer) expression calls the function.

sebor@roguewavecom at 2007-7-7 2:40:08 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 3

>

> Can I report this one as a bug?

If you have a service contract with Sun, you can file a bug report via your Sun Service representative, be kept informed of progress, and get early access to a compiler patch when the bug is fixed. More information here:

http://developers.sun.com/services/

Otherwise, you can file a bug at

http://bugs.sun.com

clamage45 at 2007-7-7 2:40:08 > top of Java-index,Development Tools,Solaris and Linux Development Tools...