R
Rennie deGraaf
Hello,
What are the rules concerning how to use a string as a template
parameter? I assume that the reason why it is necessary to use a named
object rather than a string literal is that the compiler needs to be
able to convert the value into an integer (or something similar), but I
haven't been able to make any sense about how it handles extern and const.
For instance, consider the following code:
typedef const wchar_t Bar[];
//typedef wchar_t Bar[];
//typedef const wchar_t* Bar;
//typedef const wchar_t* const Bar;
extern Bar bar = L"bar";
//Bar bar = L"bar";
template <Bar>
struct Foo {};
Foo<bar> foo;
int main()
{
return 0;
}
When using the first definition of Bar (as a const array) and the first
definition of bar (as extern), it compiles*. When Bar is non-const (the
second definition), then it compiles whether or not bar is extern
(although I get a warning if it /is/ extern). But when I use const and
/not/ extern, it doesn't compile. Why?
Also, why won't it work when I declare Bar as a pointer rather than an
array? I must admit to never having entirely understood the difference
between the two forms of declaration.
Incidentally, why can't I typedef Bar as "extern const wchar_t Bar[]"?
Thanks,
Rennie
* using either GCC 4.4.1 or Microsoft Visual Studio 2008
What are the rules concerning how to use a string as a template
parameter? I assume that the reason why it is necessary to use a named
object rather than a string literal is that the compiler needs to be
able to convert the value into an integer (or something similar), but I
haven't been able to make any sense about how it handles extern and const.
For instance, consider the following code:
typedef const wchar_t Bar[];
//typedef wchar_t Bar[];
//typedef const wchar_t* Bar;
//typedef const wchar_t* const Bar;
extern Bar bar = L"bar";
//Bar bar = L"bar";
template <Bar>
struct Foo {};
Foo<bar> foo;
int main()
{
return 0;
}
When using the first definition of Bar (as a const array) and the first
definition of bar (as extern), it compiles*. When Bar is non-const (the
second definition), then it compiles whether or not bar is extern
(although I get a warning if it /is/ extern). But when I use const and
/not/ extern, it doesn't compile. Why?
Also, why won't it work when I declare Bar as a pointer rather than an
array? I must admit to never having entirely understood the difference
between the two forms of declaration.
Incidentally, why can't I typedef Bar as "extern const wchar_t Bar[]"?
Thanks,
Rennie
* using either GCC 4.4.1 or Microsoft Visual Studio 2008