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