What are the formal grammatical constituents of object type in C++? In
particular, is `extern' part of the type? The reason I ask is because the
following code won't compile without specifying the char const[] to be
extern. Looking at the Standerd suggests to me that
storage-class-specifier is /not/ part of type-specifier. The example in
§14.3.2 suggest to me that it should compile.
template<class T, char* pclass X {
// ...
X();
X(const char* q) { /* ... */ }
};
X<int,"Studebaker"x1; // error: string literal as template-argument
char p[] = "Vivisectionist";
X<int,px2; // OK
I really don't care if my compiler is a bit non-compliant in this regard.
It's probably keeping me out of trouble if it is. I just feel as though I
should be able to resolve such a question by formally examining the
standard. Here's the code:
#include <iostream>
struct Virtue {
virtual std::ostream& print( std::ostream& out )const
{ return out<<"I am a struct called Virtue" <<std::endl; }
};
template <const char* T>
struct Temporal: public Virtue {
std::ostream& print( std::ostream& out )const
{ return out<<"I am a an instantiated template instance with
T="<<T<<std::endl; }
};
struct Stubborn: public Virtue {
template <const char* T>
std::ostream& print( std::ostream& out )const
{ return out<<"I am just plain "<<T<<std::endl; }
};
std::ostream& operator<<( std::ostream& out, const Virtue& v ) { return
v.print(out); }
//won't compile unless these are extern
extern char const text[]= "Temporal 1";
extern char const stub[]= "Stubborn";
int main(){
Virtue v;
Temporal <text>t;
Stubborn s;
std::cout<<v<<t<<s;
}
Here's the complaint I get if I remove extern from the first array
definition:
g++ -o contrived main.cpp
main.cpp: In function ?int main()?:
main.cpp:65: error: ?text? cannot appear in a constant-expression
main.cpp:65: error: template argument 1 is invalid
main.cpp:65: error: invalid type in declaration before ?;? token
Opinions?
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/