S
Steven T. Hatton
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* p> class X {
// ...
X();
X(const char* q) { /* ... */ }
};
X<int,"Studebaker"> x1; // error: string literal as template-argument
char p[] = "Vivisectionist";
X<int,p> x2; // 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:stream& print( std:stream& out )const
{ return out<<"I am a struct called Virtue" <<std::endl; }
};
template <const char* T>
struct Temporal: public Virtue {
std:stream& print( std:stream& 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:stream& print( std:stream& out )const
{ return out<<"I am just plain "<<T<<std::endl; }
};
std:stream& operator<<( std:stream& 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?
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* p> class X {
// ...
X();
X(const char* q) { /* ... */ }
};
X<int,"Studebaker"> x1; // error: string literal as template-argument
char p[] = "Vivisectionist";
X<int,p> x2; // 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:stream& print( std:stream& out )const
{ return out<<"I am a struct called Virtue" <<std::endl; }
};
template <const char* T>
struct Temporal: public Virtue {
std:stream& print( std:stream& 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:stream& print( std:stream& out )const
{ return out<<"I am just plain "<<T<<std::endl; }
};
std:stream& operator<<( std:stream& 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?