Re: (Why) Can't char* template parameters be const?

From: Steven T. Hatton (susudata_at_setidava.kushan.aa)
Date: 10/16/04


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


Relevant Pages

  • Re: (Why) Cant char* template parameters be const?
    ... > Not sure why template parameters must have external linkage though. ... Imagine you have two source files. ... Both of them define the same template. ... without violating the One Definition Rule. ...
    (comp.lang.cpp)
  • Re: Is it possible to compile and run c programs in the visual C++
    ... Is it possible to create a new project with the template main function created a C file? ... project with both C and C++ source files? ... extern "C" {#endif ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Pointer to extern char?
    ... How can there be a pointer to extern char? ... that the function has external linkage? ... static int a, *b, c; ... linkage when a declaration occurs at file scope, ...
    (comp.lang.c)
  • Re: extern
    ... The extern keyword usually gives you external ... % grep var a.c b.c ... Both have external linkage, but the "extern" in b.c inhibits ... "common block of size sizeof" will see the Fortran COMMON ...
    (comp.lang.c)
  • Re: how to use the keyword extern in c?
    ... > The extern keyword specifies that the identifier being declared has ... > external linkage. ... > includes) and referred to by name from code in other translation ... what's the difference when the keyword "extern" appears and when it doest appear? ...
    (comp.lang.c)