In the following code, lets say I want to call a specific function template instantiation based on a runtime value. I am going to need to do this a bunch of times for different functions, so I wanted to wrap the conditionals in a function (test) and pass the function template "func". So I wrap the "func" template in a struct…like so:
#include<iostream>
#include<typeinfo>
struct wrapper
{
public:
template< typename T >
static int funcT( int a )
{
std::cout<<"your func type and value is "<<typeid(T).name()<<" "<<a<<std::endl;
}
static int func( int a )
{
std::cout<<"your func type and value is "<<typeid(int).name()<<" "<<a<<std::endl;
}
};
enum class TypeIDs
{
real8_id,
int4_id
};
template< typename WRAPPER, typename RTYPE, typename...ArgsF >
static RTYPE test( const TypeIDs type, ArgsF... args )
{
RTYPE junk = WRAPPER::func(args... );
RTYPE rval;
switch( type )
{
case( TypeIDs::real8_id ):
{
rval = typename WRAPPER::funcT<double>(args...);
break;
}
case( TypeIDs::int4_id ):
{
rval = WRAPPER::funcT<int>(args... );
break;
}
}
return rval;
}
int main()
{
wrapper::funcT<double>(1);
test<wrapper,int>(TypeIDs::real8_id, 1);
}
compilation results in:
g++48 -std=c++11 templatesandfunctions.cpp
templatesandfunctions.cpp: In function 'RTYPE test(TypeIDs, ArgsF ...)':
templatesandfunctions.cpp:47:37: error: expected '(' before '<' token
rval = typename WRAPPER::funcT<double>(args...);
^
templatesandfunctions.cpp:47:38: error: expected primary-expression before 'double'
rval = typename WRAPPER::funcT<double>(args...);
^
templatesandfunctions.cpp:47:38: error: expected ';' before 'double'
templatesandfunctions.cpp:52:29: error: expected primary-expression before 'int'
rval = WRAPPER::funcT<int>(args... );
^
templatesandfunctions.cpp:52:29: error: expected ';' before 'int'
So:
wrapper::funcT<double>(1)
called from main compiles…as expected.
From the call
test<wrapper,int>(TypeIDs::real8_id, 1);"
The non-templated function
WRAPPER::func(args... );
does compile.
However the templated function does not compile with and without the typename specifier.
WRAPPER::funcT<double>(args…);
typename WRAPPER::funcT<double>(args…);
Anyone know why this doesn't work…and how to make it work?
Thanks!