Function Pointer as Template Parameter in SunStudio C++

//extern "C" void foo (void*);
extern void foo (void*);

template <typename T, void(*cb)(T*)> class Test {};
typedef Test<void, foo> TestVoid;

SunStudio C++ compiler will fail if I try to use the 1st prototype, and reports:

line 5: Error: Template parameter cb requires an expression of type void(*)(void*).
1 Error(s) detected.

No idea why this happens... 

4 thoughts on “Function Pointer as Template Parameter in SunStudio C++

  1. That's easy, in standard c++ (g++ does not conform to the standard here) the linkage ("C" or "C++") is part of the type. So you can't use an extern "C" function as a "C++" function. Usually you can use something like:
    extern "C" typedef void(*mytype)(void*);
    and replace void(*cb)(void*) by mytype cb, but here with the template parameter things are a bit harder...

  2. Hi, Marc, thank you very much for the info, I really learned that extern "C" is part of the type definition. Actually, I also guessed the "C" linkage is the root cause, and I also know that someone would eventually tell me it's the expected behavior that conforming C++ standards (I don't mean to offend you) ;)

  3. I still think you should report it as a bug to the compiler team (as a Sun employee you probably have an internal way to do that, otherwise bugs.sun.com or http://forum.java.sun.com/forum.jspa?forumID=850 are better places than opensolaris mailing-lists). The documentation of sun studio says the compiler distinguishes between "C" and "C++" functions but accepts conversions from "C" to "C++", and refusing this conversion in a template context where there is no workaround seems like a problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

To submit your comment, click the image below where it asks you to...
Clickcha - The One-Click Captcha