Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Have you ever write template function that only needs type instead of object?

Compiler

Have you ever write template function that only needs type instead of object?

by  hcexi  Posted    (Edited  )
very simple example:

Code:
template < class Q >
size_t getSize()
{
    return sizeof( Q );
}

It seems that in a case of multiple use of such function compiler generates incorrect argument type passed. Check this:

Code:
int main()
{
	std::cout << getSize< char >() << " : " << sizeof( char ) << std::endl;
	std::cout << getSize< int >() << " : " << sizeof( int ) << std::endl;
	std::cout << getSize< double >() << " : " << sizeof( double ) << std::endl;

	return 0;
}

From my point of view it's a compiler bug and I do recommend in such case to rewrite fuction into this format:

Code:
template< class Q >
size_t getSize( Q* q=0 )
{
	return sizeof( Q );
}
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top