T
Thomas Matthews
Hi,
I'm puzzled over the difference between:
extern const char *
and
extern const char [].
My understanding is that the two are equivalent,
but the program below shows elsewise.
Given the simple program below:
// File names.cpp
extern const char Hello_Text[] = "Hello";
extern const char World_Text[] = "World!";
// File main.cpp
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
template <const char * name>
void print_text(void) // personal pref. here
{
cout << name << endl;
return;
}
/* Refer to variables in names.cpp */
/* Note declaration of char * here */
/* versus char [] in names.cpp. */
extern const char * Hello_Text;
extern const char * World_Text;
int main(void)
{
cout << "Start of Test" << endl;
print_text<Hello_Text>();
print_text<World_Text>();
cout << "End of Test" << endl;
return EXIT_SUCCESS;
}
When compiled with Gnu G++ 3.3.1, I get the following
error message (line numbers omitted):
$ g++ -o main.cpp extern_names.cpp
In function `int main()':
error: `Hello_Text' is not a valid template argument
it must be the address of an object with
external linkage
no matching function for call to
`print_text()'
error: `World_Text' is not a valid template argument
it must be the address of an object with
external linkage
no matching function for call to
`print_text()'
I know that the following two statments are different:
(1) extern const char text[] = "Hello";
(2) extern const char * text = "Hello";
because (1) allocates an array of 6 locations and
(2) creates a pointer to a 'C' string literal.
But because the variable 'text' above in (1) is an
array and it has a location, a pointer can be created
and assigned to the location of the first character.
So, I would expect that
extern const char * text;
would be a pointer that refers to the start of the
character array, much the same as this statment:
extern const char text[];
So why are they different when used as template
parameters?
Does Chris Torek's "The Rule" apply here?
http://web.torek.net/torek/c/pa.html
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
I'm puzzled over the difference between:
extern const char *
and
extern const char [].
My understanding is that the two are equivalent,
but the program below shows elsewise.
Given the simple program below:
// File names.cpp
extern const char Hello_Text[] = "Hello";
extern const char World_Text[] = "World!";
// File main.cpp
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
template <const char * name>
void print_text(void) // personal pref. here
{
cout << name << endl;
return;
}
/* Refer to variables in names.cpp */
/* Note declaration of char * here */
/* versus char [] in names.cpp. */
extern const char * Hello_Text;
extern const char * World_Text;
int main(void)
{
cout << "Start of Test" << endl;
print_text<Hello_Text>();
print_text<World_Text>();
cout << "End of Test" << endl;
return EXIT_SUCCESS;
}
When compiled with Gnu G++ 3.3.1, I get the following
error message (line numbers omitted):
$ g++ -o main.cpp extern_names.cpp
In function `int main()':
error: `Hello_Text' is not a valid template argument
it must be the address of an object with
external linkage
no matching function for call to
`print_text()'
error: `World_Text' is not a valid template argument
it must be the address of an object with
external linkage
no matching function for call to
`print_text()'
I know that the following two statments are different:
(1) extern const char text[] = "Hello";
(2) extern const char * text = "Hello";
because (1) allocates an array of 6 locations and
(2) creates a pointer to a 'C' string literal.
But because the variable 'text' above in (1) is an
array and it has a location, a pointer can be created
and assigned to the location of the first character.
So, I would expect that
extern const char * text;
would be a pointer that refers to the start of the
character array, much the same as this statment:
extern const char text[];
So why are they different when used as template
parameters?
Does Chris Torek's "The Rule" apply here?
http://web.torek.net/torek/c/pa.html
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library