473,480 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Grammatical constituents of object type.

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/
Dec 5 '06 #1
2 1439
Steven T. Hatton <ch********@germania.supwrote:
>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?
My opinion:

The combined use of "const" and a constant initializer (in this
case a string) tells the compiler to go ahead and substitute
the initializer for any instances of the variable in the file
being compiled (in the manner of #define). Once it's done this, you
run into the rule (discussed in the "local classes" thread) that
parameters of a template instantiation must have external linkage.

Steve
Dec 5 '06 #2
Steve Pope wrote:
My opinion:

The combined use of "const" and a constant initializer (in this
case a string) tells the compiler to go ahead and substitute
the initializer for any instances of the variable in the file
being compiled (in the manner of #define). Once it's done this, you
run into the rule (discussed in the "local classes" thread) that
parameters of a template instantiation must have external linkage.

Steve
Thank you. Removing the const qualifier also allowed the program to
compile.

http://publib.boulder.ibm.com/infoce...ef/cplr062.htm

"In C++ a global const object without an explicit storage class is
considered static by default, with internal linkage."

That seems to explain the behavior. It also explains something else I have
puzzled over for quite some time. From the Standard: "Although entities in
an unnamed namespace might have external linkage, they are effectively
qualified by a name unique to their translation unit and therefore can
never be seen from any other translation unit." I found that I had to make
my const char[] external, but that cause ODR violations unless I also put
it in an unnamed namespace. What I have yet to attempt is putting the
definition in a single implementation file, which I have read, is the
preferable method of resolving this problem.

I have to say this aspect of C++ falls in second place to Cpp as a major
source of stumbling block in my learning C++. I am finally beginning to
understand what's actually happening.
--
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/
Dec 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4750
by: Avi Kak | last post by:
Hello: Please forgive me if my question is too silly or just not well-formed. Wesley Chun in his book (Core Python Programming) says that **everything** in Python is an object. So I became...
44
2393
by: Steven T. Hatton | last post by:
This may seem like such a simple question, I should be embarrassed to ask it. The FAQ says an object is "A region of storage with associated semantics." OK, what exactly is meant by "associated...
5
1320
by: Steven T. Hatton | last post by:
I often find myself at a loss to find the correct words to identify the parts of a C++ statement. I know you can pick up a lot by reading the Standard. That may indeed be the best available route...
16
25389
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
100
5152
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
5
2251
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
16
2869
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
2
2618
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
5
3153
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6911
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7091
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6743
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.