473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Specialization of template functions

Hello,

I have a small problem I want to demonstrate with a small demo program:

-------------- Snip --------------

#include <string>

namespace
{
template< bool I >
struct StaticAssert
{
};

template<>
struct StaticAssert< true >
{
enum { value = 1 };
};

#define STATIC_ASSERT( V ) StaticAssert< V >::value

template< typename T >
std::string dump( const T& value )
{
STATIC_ASSERT( false );
}

template<>
std::string dump( const std::string& value )
{
return value;
}
}

int main( const int argc,
char* argv[] )
{
const std::string text = "Hello World!";
dump( text );

return 0;
}

-------------- Snip --------------

(The upper part is like the BOOST_STATIC_ASSERT.)

This can be compiled perfectly with the GCC 3.3 compiler, but not with
GCC 4.0 or 4.1. I want to keep this code because it causes a compiler
error when the template function is called and not one of the
specialized ones.

Has anybody an idea?

--
Greetings,
Markus
Jul 29 '06 #1
7 1600
Markus Petermann <ma********************@gmx.netwrote:
#include <string>

namespace
{
template< bool I >
struct StaticAssert
{
};

template<>
struct StaticAssert< true >
{
enum { value = 1 };
};

#define STATIC_ASSERT( V ) StaticAssert< V >::value

template< typename T >
std::string dump( const T& value )
{
STATIC_ASSERT( false );
}

template<>
std::string dump( const std::string& value )
Don't you need to write

template <>
std::string dump <std::string( const std::string& value )

?
{
return value;
}
}

int main( const int argc,
char* argv[] )
{
const std::string text = "Hello World!";
dump( text );

return 0;
}
This can be compiled perfectly with the GCC 3.3 compiler, but not with
GCC 4.0 or 4.1.
What is the error message?

hth
--
jb

(reply address in rot13, unscramble first)
Jul 29 '06 #2
Markus Petermann wrote:
Hello,

I have a small problem I want to demonstrate with a small demo program:

-------------- Snip --------------

#include <string>

namespace
{
template< bool I >
struct StaticAssert
{
};

template<>
struct StaticAssert< true >
{
enum { value = 1 };
};

#define STATIC_ASSERT( V ) StaticAssert< V >::value

template< typename T >
std::string dump( const T& value )
{
STATIC_ASSERT( false );
}

template<>
std::string dump( const std::string& value )
{
return value;
}
}

int main( const int argc,
char* argv[] )
{
const std::string text = "Hello World!";
dump( text );

return 0;
}

-------------- Snip --------------

(The upper part is like the BOOST_STATIC_ASSERT.)

This can be compiled perfectly with the GCC 3.3 compiler, but not with
GCC 4.0 or 4.1. I want to keep this code because it causes a compiler
error when the template function is called and not one of the
specialized ones.

Has anybody an idea?

What about:

#include <string>

namespace
{
template< bool I >
struct StaticAssert;

template<>
struct StaticAssert< true >
{};

#define STATIC_ASSERT( X ) StaticAssert< X >();

template< typename T >
std::string dump( const T& value )
{
STATIC_ASSERT( false );
return ( value );
}

template<>
std::string dump( const std::string& value )
{
return value;
}
}

int main( const int argc,
char* argv[] )
{
const std::string text = "Hello World!";
dump( text );

return 0;
}
Jul 29 '06 #3
Hello,

Jakob Bieling wrote:
>This can be compiled perfectly with the GCC 3.3 compiler, but not with
GCC 4.0 or 4.1.

What is the error message?
Sorry, I forgot. The error message is:

g++ alpha.cpp -o alpha
alpha.cpp: In function 'std::string<unnamed>::dump(const T&)':
alpha.cpp:21: error: 'value' is not a member of
'<unnamed>::StaticAssert<false>'

(Line 21 is the one with the STATIC_ASSERT( false ) macro.)

IMHO the compiler tries to compile the template method, although I call
the specialized one for strings.

--
Greetings,
Markus
Jul 29 '06 #4
Hello,

Kai-Uwe Bux wrote:
What about:
<Snip>

Great! This code can be compiled with G++ 4.0 and 4.1.

One minor problem: Your fix is in the part that is normally provided by
the boost library (the BOOST_STATIC_ASSERT macro, to be exactly). I am
going to search the boost bug reports if there were similar problems.

But with this information I can build my own static assert as a
temporary fix.

Thank you!

--
Greetings,
Markus
Jul 29 '06 #5
Kai-Uwe Bux wrote:
Markus Petermann wrote:
[...]
>
What about:

#include <string>

namespace
{
template< bool I >
struct StaticAssert;

template<>
struct StaticAssert< true >
{};

#define STATIC_ASSERT( X ) StaticAssert< X >();

template< typename T >
std::string dump( const T& value )
{
STATIC_ASSERT( false );
return ( value );
}

template<>
std::string dump( const std::string& value )
{
return value;
}
}

int main( const int argc,
char* argv[] )
{
const std::string text = "Hello World!";
dump( text );

return 0;
}

But what is the use of STATIC_ASSERT in your example ?
If I change the main into :

int main()
{
const int i = 3;
dump(i);
return 0;
}

The error I get is :

spec.cc: In function 'std::string<unnamed>::dump(const T&) [with T = int]':
spec.cc:32: instantiated from here
spec.cc:19: error: invalid conversion from 'const int' to 'const char*'
spec.cc:19: error: initializing argument 1 of
'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*,
const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>,
_Alloc = std::allocator<char>]'
make: *** [spec] Erreur 1

Thus there is no error on the STATIC_ASSERT line ...(line 19 is for me
the line of the return statement after the STATIC_ASSERT).

Pierre
Jul 30 '06 #6
Pierre Barbier de Reuille wrote:
Kai-Uwe Bux wrote:
>Markus Petermann wrote:
[...]
>>
What about:

#include <string>

namespace
{
template< bool I >
struct StaticAssert;

template<>
struct StaticAssert< true >
{};

#define STATIC_ASSERT( X ) StaticAssert< X >();

template< typename T >
std::string dump( const T& value )
{
STATIC_ASSERT( false );
return ( value );
}

template<>
std::string dump( const std::string& value )
{
return value;
}
}

int main( const int argc,
char* argv[] )
{
const std::string text = "Hello World!";
dump( text );

return 0;
}


But what is the use of STATIC_ASSERT in your example ?
If I change the main into :

int main()
{
const int i = 3;
dump(i);
return 0;
}

The error I get is :

spec.cc: In function 'std::string<unnamed>::dump(const T&) [with T =
int]':
spec.cc:32: instantiated from here
spec.cc:19: error: invalid conversion from 'const int' to 'const char*'
spec.cc:19: error: initializing argument 1 of
'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*,
const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>,
_Alloc = std::allocator<char>]'
make: *** [spec] Erreur 1

Thus there is no error on the STATIC_ASSERT line ...(line 19 is for me
the line of the return statement after the STATIC_ASSERT).
I get two error messages:

news_groupcc++ xxx.cc
xxx.cc: In function 'std::string<unnamed>::dump(const T&) [with T = int]':
xxx.cc:32: instantiated from here
xxx.cc:17: error: invalid use of undefined
type 'struct<unnamed>::StaticAssert<false>'
xxx.cc:6: error: declaration of 'struct<unnamed>::StaticAssert<false>'
xxx.cc:18: error: conversion from 'const int' to non-scalar
type 'std::basic_string<char, std::char_traits<char>, std::allocator<char>
>' requested

As you can see, the first corresponds to the STATIC_ASSERT.

Try compiling this:

#include <string>

namespace
{
template< bool I >
struct StaticAssert;

template<>
struct StaticAssert< true >
{};

#define STATIC_ASSERT( X ) StaticAssert< X >();

template< typename T >
T dump( const T& value )
{
STATIC_ASSERT( false );
return ( value );
}

template<>
std::string dump( const std::string& value )
{
return value;
}
}

int main( const int argc,
char* argv[] )
{
int const i = 3;
dump( i );
return 0;
}

I think, it should give you one and only one error, and that error should
relate to STATIC_ASSERT.
Best

Kai-Uwe Bux
Jul 30 '06 #7
Kai-Uwe Bux wrote:
Pierre Barbier de Reuille wrote:
[...]
>
Try compiling this:

#include <string>

namespace
{
template< bool I >
struct StaticAssert;

template<>
struct StaticAssert< true >
{};

#define STATIC_ASSERT( X ) StaticAssert< X >();

template< typename T >
T dump( const T& value )
{
STATIC_ASSERT( false );
return ( value );
}

template<>
std::string dump( const std::string& value )
{
return value;
}
}

int main( const int argc,
char* argv[] )
{
int const i = 3;
dump( i );
return 0;
}

I think, it should give you one and only one error, and that error should
relate to STATIC_ASSERT.
Indeed, I forgot to remove completely the body of the main template
declaration of StaticAssert :/

Thanks,

Pierre
>

Best

Kai-Uwe Bux
Jul 30 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
9
by: Patrick Kutch | last post by:
Please take a look at the following code snippit: ------------------------------------------------------- template<class TraitClass=int> class TestClass { public: void CallFunc() {...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
7
by: Kai-Uwe Bux | last post by:
Hi folks, I observed something that puzzles me. When I do namespace xxx { using std::swap; } it appears that xxx::swap and std::swap are not strictly equivalent. In particular, I think...
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
10
by: jason.cipriani | last post by:
I never seem to be able to get this right. Here I have some code: template <typename T, int Nclass A { void f (T); }; template <typename Tvoid A<T,1>::f (T) { } template <typename Tvoid...
1
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.