PDA

View Full Version : gcc problem: c++ tmplates



stinos
31st August 2007, 10:14
Hi All,

I'm currently using gcc 4.20 and I'm running into problems with templates. Afaik the latest gcc versions are quit strict in meeting the standard, so I'm probably doing something wrong, but I don't see what.. (reading the standard didn't help, the lingo is quite weird ;-)

here's a simplified test case:


template< class T >
struct tTest
{
template< class T2 >
static T2 func()( const T& ac_tType )
{
//actual code does stuff to convert T->T2 here
return T2();
}
};

template< class T >
struct tUsesTest
{
typedef tTest< T > mt_Test;

void func()
{
T test;
int nConverted = mt_Test::func( test );
}
};


int main( argc, argv )
{
tUsesTest< unsigned > test;
test.func();
return 0;
}


this gives:
20: error: no matching function for call to 'tTest<unsigned int>::func(unsigned int&)'

replacing line 20 with


int nConverted = mt_test::func< int >( test );


yields:
20: error: expected primary-expression before 'int'

Can anyone explain in understandable language why this fails, and how to code this correctly?
Thanks in advance!

marcel
31st August 2007, 17:15
template< class T >
struct tTest
{
template< class T2 >
static T2 func( const T& ac_tType )
{
//actual code does stuff to convert T->T2 here
return T2();
}
};

template< class T >
struct tUsesTest
{
typedef tTest< T > mt_Test;

void func()
{
T test;
int nConverted = mt_Test::func<unsigned int>( test );
}
};

int main(int argc, char* argv[])
{
tUsesTest< int > test;
test.func();
return 0;
}


You had a couple of errors back there. For example you did not specify the T2 type for func in tUsesTest. So the compiler could not deduce the type out of thin air
.
And in tTest, func had a weird syntax. It did resembled a bit with pointer to function syntax, but it wasn't correct.

Try the code I posted. Should work with gcc too.

stinos
31st August 2007, 19:31
You had a couple of errors back there. For example you did not specify the T2 type for func in tUsesTest. So the compiler could not deduce the type out of thin air
.
And in tTest, func had a weird syntax. It did resembled a bit with pointer to function syntax, but it wasn't correct.


ok sorry for the typos..



Try the code I posted. Should work with gcc too.

well that's the same as what I did here


int nConverted = mt_test::func< int >( test );


and that doesn't work with gcc (tried both 4.0.1 and 4.2).. it does with msvc8.0, but I'm not sure which of the two is correct here.

marcel
31st August 2007, 19:39
Try this:



int nConverted = mt_Test::template func<unsigned int>( test );
You got your error because func is dependent on the template type, which is resolved at instantiation time( your function is static.

BTW, you can use ->template when accessing template functions for pointers and .template for objects.

These cool things have been introduced in the standard by necessity( my opinion ).

regards

stinos
1st September 2007, 13:48
well thanks alot! that does indeed do the trick!