, ,...
... idiom,
template< class Dummy >
struct Foo_
{
static char const s[];
};
template< class Dummy >
char const Foo_<Dummy>::s[] = "Blah blah";
typedef Foo_<void> Foo;
#include <iostream>
using namespace std;
int main()
{
cout << sizeof( Foo::s ) << " bytes: \"" << Foo::s << "\"\n";
}
.
, , char/wchar_t-agnostic , , .
:
MSVC 7.1 10.0 sizeof. , g++ 4.4.1, Comeau Online 4.3.10.1, MSVC 7.1 MSVC 10.0.
#include <stddef.h>
typedef ptrdiff_t Size;
template< Size n >
struct SizedBuf { char sizer[n]; };
template< class Type, Size n >
SizedBuf< n > countOf_( Type (&)[n] ) { return n; }
#define COUNT_OF( array ) sizeof( countOf_( array ).sizer )
#define DEF_STRING( name, value ) \
template< class > \
struct name##_constant_ \
{ \
static char const str[]; \
static Size const length = COUNT_OF( value ) - 1; \
}; \
\
template< class Type > \
char const name##_constant_< Type >::str[] = value; \
\
template< class Type > \
Size const name##_constant_< Type >::length; \
\
typedef name##_constant_<void> name;
DEF_STRING( a, "Argh, MSVC!" )
DEF_STRING( b, "Blah blah" )
DEF_STRING( c, "Currently there no 'inline' for data in C++." )
#include <iostream>
template< char const* s >
void foo() { std::cout << "foo() says: " << s << std::endl; }
int main()
{
using namespace std;
int const x[a::length] = {};
foo<a::str>();
cout << a::length << " characters: \"" << a::str << "\"." << endl;
}
hth.,