namespaces conflict?

A

Aleks D

Take a look at the following code:

std::istream& operator>>(std::istream& in, char* str) {
int i=0;
for (char c; str && in.get(c) && c==str; ++i)
; //get consumes the characters from the stream
if (str) {
in.setstate(std::ios_base::failbit);
std::cerr<<"problem with consuming"<<str; //debugging code
}
return in;
};

namespace SimpleSketch {

istream& operator >>(std::istream& is, SimpleGraph& graph) {
is>>"ImgWd = "; //line A
/* .... */
}
}

This code crashes on line A. But if I put the definition of the
operator >> inside the namespace SimpleSketch, it works.
Unfortunately the code is in another file, so that makes things highly
inconvenient. How can I make the caller code work without modifying or
copying the source of the operator.

I am programming on MSVC++.NET

Cheers,
Aleks D.

P.S. this relates to a post from a couple of months ago.
 
M

Mike Wahler

Aleks D said:
Take a look at the following code:

std::istream& operator>>(std::istream& in, char* str) {
int i=0;
for (char c; str && in.get(c) && c==str; ++i)
; //get consumes the characters from the stream
if (str) {
in.setstate(std::ios_base::failbit);
std::cerr<<"problem with consuming"<<str; //debugging code
}
return in;
};

namespace SimpleSketch {

istream& operator >>(std::istream& is, SimpleGraph& graph) {
is>>"ImgWd = "; //line A


This is an attempt to modify a string literal.
/* .... */
}
}

This code crashes on line A.

I'm not surprised.

But if I put the definition of the
operator >> inside the namespace SimpleSketch, it works.

It's not required to.
Unfortunately the code is in another file, so that makes things highly
inconvenient. How can I make the caller code work without modifying or
copying the source of the operator.

Do not do things that produce undefined behavior.

-Mike
 
R

Rob Williscroft

Aleks D wrote in in
comp.lang.c++:
Take a look at the following code:

std::istream& operator>>(std::istream& in, char* str) {

You don't modify the str argument so make it char const *.
int i=0;
for (char c; str && in.get(c) && c==str; ++i)
; //get consumes the characters from the stream
if (str) {
in.setstate(std::ios_base::failbit);
std::cerr<<"problem with consuming"<<str; //debugging code
}
return in;
};

namespace SimpleSketch {

istream& operator >>(std::istream& is, SimpleGraph& graph) {
is>>"ImgWd = "; //line A
/* .... */
}
}

This code crashes on line A.


What's the crash

....

Ok got it, you're using the compiler in a non conforming mode
and its treating "ImgWd = " as a char *, it should be a const.

Then the compiler is calling std::eek:perator >>( ... , char * ) which
overwrites the constant string and you get a crash, this is conforming
behaviour BTW.
But if I put the definition of the
operator >> inside the namespace SimpleSketch, it works.
Unfortunately the code is in another file, so that makes things highly
inconvenient. How can I make the caller code work without modifying or
copying the source of the operator.

I am programming on MSVC++.NET

#include <iostream>
#include <sstream>

std::istream& operator>>(std::istream& in, char const * str)
{
int i=0;
for (char c; str && in.get(c) && c==str; ++i)
; //get consumes the characters from the stream
if (str) {
in.setstate(std::ios_base::failbit);
std::cerr<<"problem with consuming"<<str; //debugging code
}
return in;
};


int main()
{
std::istringstream iss( "Key = value" );


iss >> "Key = ";

char buf[ 10 ];
if ( !(iss >> buf) )
{
std::cout << "Arghhhhhhhh........\n";
}
else
{
std::cout << "[" << buf << "]\n";
}
}

The above works but I don't recomend it, be more explicit instead:

struct scanable
{
char const *str;
scanable( char const *s ) : str( s ) {}
};

std::istream operator >> ( std::istream &is, scanable const &scan )
{
// same as above but with scan.str substituted for str.
}

HTH.

Rob.
 

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

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,512
Latest member
FinleyNick

Latest Threads

Top