Undefined constructor of std::string

O

Old Wolf

My compiler's <string> header has this constructor
(after preprocessing):

template <class charT, class traits , class Allocator >
class basic_string
{
template <class InputIterator>
basic_string (InputIterator, InputIterator,
const Allocator& = Allocator()));

//.........
};

That means that some code like:
unsigned char a[10];
std::string s(a, a+10);

does not give a compiler error, but it does give a link error
because no body is ever defined for that constructor.

Should there be a body, or should this constructor not be
present at all? I have worked around it by writing my own
function body into <string> .
 
V

Victor Bazarov

Old said:
My compiler's <string> header has this constructor
(after preprocessing):

template <class charT, class traits , class Allocator >
class basic_string
{
template <class InputIterator>
basic_string (InputIterator, InputIterator,
const Allocator& = Allocator()));

//.........
};

That means that some code like:
unsigned char a[10];
std::string s(a, a+10);

does not give a compiler error, but it does give a link error
because no body is ever defined for that constructor.

Should there be a body, or should this constructor not be
present at all? I have worked around it by writing my own
function body into <string> .

You should avoid manually making changes to your compiler headers.
Perhaps you should contact your compiler vendor and find out why
your code isn't working. As to the body, the Standard does not
require any particular location of it anywhere. For all we know
you may be forgetting to include a particular C++ library when doing
the linking.

V
 
K

Krishanu Debnath

Old said:
My compiler's <string> header has this constructor
(after preprocessing):

template <class charT, class traits , class Allocator >
class basic_string
{
template <class InputIterator>
basic_string (InputIterator, InputIterator,
const Allocator& = Allocator()));

//.........
};

That means that some code like:
unsigned char a[10];
std::string s(a, a+10);

does not give a compiler error, but it does give a link error
because no body is ever defined for that constructor.

No, any standard compliant compiler should support this. gcc 3.2 does
support this. Sec 21.3 [lib.basic.string] specify this constructor.
Should there be a body, or should this constructor not be
present at all? I have worked around it by writing my own
function body into <string> .

I firmly believe that you know this is not a good idea. :)

Krishanu
 
O

Old Wolf

Krishanu said:
Old said:
That means that some code like:
unsigned char a[10];
std::string s(a, a+10);

does not give a compiler error, but it does give a link error
because no body is ever defined for that constructor.

No, any standard compliant compiler should support this.
Sec 21.3 [lib.basic.string] specify this constructor.
Should there be a body, or should this constructor not be
present at all? I have worked around it by writing my own
function body into <string> .

I firmly believe that you know this is not a good idea. :)

Not great, no, but contacting the compiler vendor is a far more
time-consuming and expensive option than my 2-minute fix.

I suppose the best thing to do might be to modify my code so
that it doesn't use on that constructor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top