Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function pointer as template argument - does not work for function template

Reply
Thread Tools

Function pointer as template argument - does not work for function template

 
 
avasilev
Guest
Posts: n/a
 
      12-21-2011
HI all,
Can anyone tell me why this c ode does not compile on MSVC2008, but
compiles fine with GCC4. Are there any differences related to function
pointer template arguments for class templates and func templates?

template <typename Sig>
struct FuncSig;

template<typename RV, typename A1>
struct FuncSig<RV(A1)>
{ typedef RV(*Type)(A1); };

//Class template with the help of the signature class
template <typename Sig, typename FuncSig<Sig>::Type Func>
class Test{};

//function template with the help of the signature class
template <typename Sig, typename FuncSig<Sig>::Type Func>
int TestFunc()
{ return 123; }

//some random fucntion to pass as a pointer
int func(char a)
{
return 5;
}

Test<int(char), &func> f; //compiles as expected

int main()
{

//compiles on GCC, but fails on MSVC2008, with error messge:
//templateFuncAndClassArgValidity.cpp(27) : error C2975: 'Func' :
invalid template argument for //'TestFunc', expected compile-time
constant expression
// templateFuncAndClassArgValidity.cpp(13) : see declaration of
'Func'

TestFunc<int(char), &func>();
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-21-2011
On 12/21/2011 3:08 PM, avasilev wrote:
> template<typename Sig>
> struct FuncSig;
>
> template<typename RV, typename A1>
> struct FuncSig<RV(A1)>
> { typedef RV(*Type)(A1); };
>
> //Class template with the help of the signature class
> template<typename Sig, typename FuncSig<Sig>::Type Func>
> class Test{};
>
> //function template with the help of the signature class
> template<typename Sig, typename FuncSig<Sig>::Type Func>
> int TestFunc()
> { return 123; }
>
> //some random fucntion to pass as a pointer
> int func(char a)
> {
> return 5;
> }
>
> Test<int(char),&func> f; //compiles as expected
>
> int main()
> {
>
> //compiles on GCC, but fails on MSVC2008, with error messge:
> //templateFuncAndClassArgValidity.cpp(27) : error C2975: 'Func' :
> invalid template argument for //'TestFunc', expected compile-time
> constant expression
> // templateFuncAndClassArgValidity.cpp(13) : see declaration of
> 'Func'
>
> TestFunc<int(char),&func>();
> }


Most likely it's a bug in VC2008 (still exists in VC2010). A pointer to
a function is allowed to be used as a non-type template argument if the
function has external linkage (as your 'func' apparently does). See
[temp.arg.nontype]/1, third bullet.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      12-22-2011
On 21.12.2011 23:31, Victor Bazarov wrote:
>> //compiles on GCC, but fails on MSVC2008, with error messge:
>> //templateFuncAndClassArgValidity.cpp(27) : error C2975: 'Func' :
>> invalid template argument for //'TestFunc', expected compile-time
>> constant expression
>> // templateFuncAndClassArgValidity.cpp(13) : see declaration of
>> 'Func'
>>
>> TestFunc<int(char),&func>();
>> }

>
> Most likely it's a bug in VC2008 (still exists in VC2010). A pointer to
> a function is allowed to be used as a non-type template argument if the
> function has external linkage (as your 'func' apparently does). See
> [temp.arg.nontype]/1, third bullet.


I remember a similar bug in VS2003. AFAIR you can't use any linker
symbols as template arguments. I had a smart pointer class, that used a
singleton in case a NULL value is dereferenced. The singleton is passed
as template argument.

There was a work-around, maybe it helps here too. Define a constant that
is inilialized to your function pointer and use this one as template
argument.


Marcel
 
Reply With Quote
 
avasilev
Guest
Posts: n/a
 
      12-22-2011
Interestingly, though, this compiles (on both compilers):

template <typename Sig>
struct FuncSig;

template<typename RV, typename A1>
struct FuncSig<RV(A1)>
{ typedef RV(*Type)(A1); };

template <class C, typename Sig>
struct MethodSig;

template<class C, typename RV, typename A1>
struct MethodSig<C, RV(A1)>
{ typedef RV(C::*Type)(A1); };

//Class template with the help of the signature class
template <typename Sig, typename FuncSig<Sig>::Type Func>
class Test{};

//Class template with the help of the signature class
template <class C, typename Sig, typename MethodSig<C, Sig>::Type
Method>
class TestM{};

//function template with the help of the signature class
template <typename Sig, typename FuncSig<Sig>::Type Func>
int TestFunc()
{ return 123; }

//function template with the help of the signature class
template <class C, typename Sig, typename MethodSig<C, Sig>::Type
Method>
int TestFuncM()
{ return 123; }

//some random fucntion to pass as a pointer
int func(char a)
{
return 5;
}

struct MyClass
{ int method(char a) {return 5;} };

Test<int(char), &func> f; //compiles as expected
TestM<MyClass, int(char), &MyClass::method> m;

int main()
{
TestFuncM<MyClass, int(char), &MyClass::method>();
}

 
Reply With Quote
 
avasilev
Guest
Posts: n/a
 
      12-22-2011
> I remember a similar bug in VS2003. AFAIR you can't use any linker
> symbols as template arguments. I had a smart pointer class, that used a
> singleton in case a NULL value is dereferenced. The singleton is passed
> as template argument.
>
> There was a work-around, maybe it helps here too. Define a constant that
> is inilialized to your function pointer and use this one as template
> argument.
>


It seems this is not the problem - I can do it freely with class
templates, but not with function templates. In the second example I
posted, it works with a func template class as well, but only with a
method signature.
In your case, did you try to pass NULL as a template func pointer
argument? If so, the case is different, I also have tried to do it,
but the pre-C++0x standard prohibits this explicitly
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
function argument dependent on another function argument? Reckoner Python 11 01-19-2009 03:31 AM
Template argument as template argument nw C++ 0 04-14-2008 01:36 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
Template function as argument to another template function Jim West C++ 3 10-07-2004 07:21 PM



Advertisments