Re: namespaces conflict?

From: Rob Williscroft (rtw_at_freenet.co.uk)
Date: 10/20/04


Date: 20 Oct 2004 04:33:41 GMT

Aleks D wrote in news:820e57f0.0410191959.5e79fbab@posting.google.com 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[i] && in.get(c) && c==str[i]; ++i)
> ; //get consumes the characters from the stream
> if (str[i]) {
> 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::operator >>( ... , 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[i] && in.get(c) && c==str[i]; ++i)
                ; //get consumes the characters from the stream
        if (str[i]) {
                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.

-- 
http://www.victim-prime.dsl.pipex.com/


Relevant Pages

  • __declspec(noreturn)
    ... void throw_error(char const* str) ... std::string msg; ... The compiler complains that not all paths return something. ...
    (microsoft.public.vc.language)
  • Re: Problem using string pointer
    ... str points to the str literal. ... This attempts to modify the character str points to. ... any attempt to modify a string literal causes undefined behavior. ... One of the possible manifestations of undefined behavior is "works as ...
    (microsoft.public.vc.language)
  • Re: doubt related to string pointers.
    ... >What is the reason for that? ... It is an implementation detail whether there are one or two strings ... is perfectly legal to modify str as often as you want. ...
    (comp.lang.c)
  • Re: Store the path of images that im loading in axes1
    ... So instead of listbox I put on form a static text component and I modify the code like that: ... %on cancel press store me this message in an error dialog ... imshow; %displaying image in axes1 ... str = get; ...
    (comp.soft-sys.matlab)
  • Re: Modify a strings value
    ... modify a string's internal value? ... The 'str' type has only ... You'd have to take care of creating those values explicitly, ... Ben Finney ...
    (comp.lang.python)