Re: Constant string as template arg
On Sep 25, 2:56 pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
+use...@gmail.com> wrote:
* Marcel M=FCller, on 25.09.2010 15:23:
from time to time I used something like
template <int I> class_or_function ...
This works fine.
Today I had the first case where
template <const char* C> foo();
...
foo<"LST">();
would be useful. Unfortunately the compiler dislikes it:
error: string literal "LST" is not a valid template argument
because it is the address of an object with static linkage
Why is this not allowed?
I'm not sure. But it's the same as with a function pointer as
template parameter. It must be a function with external
linkage.
Perhaps it's just historical.
In this case, there are two reasons. The first, that a template
argument must have external linkage, probably is just historical
(but I'm not sure that the restriction is going to be removed in
the next version of the standard---I'd have to check). The
second is because whether two repetitions of "LST" are two
different objects (and thus two different instantiations of the
template) or just one is not specified, so even allowing it
wouldn't make it usable.
It's not a big deal to rewrite the code manually to get the
desired effect, and since the compiler could do that
automagically I can't see any technical reason.
The address of a static object is a constant.
And with this restriction I could never pass any string as
template argument, or die I miss something?
You'll have to name the string, like
namespace {
extern char const blah[] = "LST";
}
//...
foo<blah>();
I think that should work.
It does, at least with the compilers we use. Sort of---you do
have to work around the usual Microsoft bugs. (In this case,
I have the equivalent of:
class Base
{
protected:
Base(char const* name) { Saves name, for future use...}
};
template<char const* id>
class Derived : public Base
{
public:
Derived() : Base(id) {}
};
and the constructor of Base gets some random value, rather than
what it should get.
--
James Kanze