Re: (Why) Can't char* template parameters be const?
From: Steven T. Hatton (susudata_at_setidava.kushan.aa)
Date: 10/16/04
- Next message: David Hilsee: "Re: Placement operator new/delete question"
- Previous message: Ioannis Vranos: "Re: Placement operator new/delete question"
- In reply to: JKop: "Re: (Why) Can't char* template parameters be const?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 16 Oct 2004 02:14:17 -0400
JKop wrote:
>
>> Template parameters must have external linkage, const is implicitly
>> internal linkage in C++. Try
>>
>> extern const char name[] = "This is a Class Name";
>>
>> Not sure why template parameters must have external linkage though.
>
>
> Imagine you have two source files.
Indeed, that was the other part of the problem. When I made it extern, I
got ODR violations. I ended up doing this:
namespace sth {
namespace tmath {
namespace {
extern const char TensorIndex_CA[] = "TensorIndex";
...
using std::stringstream;
}
template<size_t Order_S, size_t Rank_S>
class TensorIndex : public NamedClass<TensorIndex_CA> {
public:
static const size_t ORDER; // range of indices
static const size_t RANK; // number of indices
static const size_t SIZE; // number of components
static string className();
static string fqClassName();
static string baseClasses();
ostream& print(ostream& out, const size_t& verbosity=0) const;
};
> Both of them define the same template (by including its header file).
>
>
> Both of them have a "const" global variable (which has internal linkage).
At worst it will be namespace local. If I were force to make them global, I
would be switching the C# right now.
> They both create a instance of this template (what's the correct lingo
> there?) using their global variable.
I believe you instantiate a template when you make a type out of it. You
would then instantiate an object of that type before you could use a class
template, etc.
> The problem here is that, because "const" makes it internal linkage, the
> two files could have different definitions of a variable of the same name,
> and without violating the One Definition Rule. As a result:
>
> Blah<monkey>();
>
> in one file, will not be the same instance of the template as:
>
> Blah<monkey>();
>
> which resides in a different source file.
I'm not sure what to make of that, because it appears I /cannot/ have the
definition visible across translation unit boundaries. So the linkage has
to be extern, but it cannot be externally visible. Go figure.
-- "If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in which we never know what we are talking about, nor whether what we are saying is true." - Bertrand Russell
- Next message: David Hilsee: "Re: Placement operator new/delete question"
- Previous message: Ioannis Vranos: "Re: Placement operator new/delete question"
- In reply to: JKop: "Re: (Why) Can't char* template parameters be const?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|