Strange behavior with default function assignment

Discussion in 'C++' started by Denis Remezov, Jun 7, 2004.

  1. BCC wrote:
    >
    > Given this test code:
    > .h file
    > --------------
    > #include <iostream>
    > #include <string>
    > class CContainer {
    > public:
    > typedef std::string::size_type size_type;
    > static const size_type npos2 = std::string::npos;
    > };
    > class CTest {
    > public:
    > void Foo(CContainer::size_type to = CContainer::npos2);
    > };
    >
    > .cpp file
    > ---------------
    > #include "TestApp.h"
    > using namespace std;
    > int main()
    > {
    > CTest ct;
    > ct.Foo();
    > return 0;
    > }
    > void CTest::Foo(CContainer::size_type to /* = CContainer::npos2 */)
    > {
    > cout << "To == " << to << endl;
    > cout << "NPOS == " << string::npos << endl;
    > }
    >
    > My output is:
    > To == 0
    > NPOS == 4294967295
    >
    > Why is To not equal to NPOS????? This snippet is from a larger chunk of
    > code that was originally compiled on solaris, and in porting it to windows
    > To is not initialized as I think it should be. Dont have a sun machine to
    > test it on though.
    >
    > Any one have any suggestions on what is going on? Easy to work around, but
    > the behavior is incorrect as far as I can tell. This was compiled under
    > .NET 2003 btw.
    >


    I've no idea if this would help, and I can't run MSVC to test this idea out,
    but I would still provide a definition for npos2 to make the program
    perfectly valid:

    const CContainer::size_type CContainer::npos2;

    Denis
     
    Denis Remezov, Jun 7, 2004
    #1
    1. Advertising

  2. Denis Remezov

    BCC Guest

    Given this test code:
    ..h file
    --------------
    #include <iostream>
    #include <string>
    class CContainer {
    public:
    typedef std::string::size_type size_type;
    static const size_type npos2 = std::string::npos;
    };
    class CTest {
    public:
    void Foo(CContainer::size_type to = CContainer::npos2);
    };

    ..cpp file
    ---------------
    #include "TestApp.h"
    using namespace std;
    int main()
    {
    CTest ct;
    ct.Foo();
    return 0;
    }
    void CTest::Foo(CContainer::size_type to /* = CContainer::npos2 */)
    {
    cout << "To == " << to << endl;
    cout << "NPOS == " << string::npos << endl;
    }


    My output is:
    To == 0
    NPOS == 4294967295

    Why is To not equal to NPOS????? This snippet is from a larger chunk of
    code that was originally compiled on solaris, and in porting it to windows
    To is not initialized as I think it should be. Dont have a sun machine to
    test it on though.

    Any one have any suggestions on what is going on? Easy to work around, but
    the behavior is incorrect as far as I can tell. This was compiled under
    ..NET 2003 btw.

    Thanks,
    Bryan
     
    BCC, Jun 8, 2004
    #2
    1. Advertising

  3. Denis Remezov

    Xiaobin Yang Guest

    > My output is:
    > To == 0
    > NPOS == 4294967295
    >
    > Why is To not equal to NPOS????? This snippet is from a larger chunk of


    on my machine, I get:
    To == 4294967295
    NPOS == 4294967295
     
    Xiaobin Yang, Jun 8, 2004
    #3
  4. Denis Remezov wrote:
    >
    > BCC wrote:
    > >
    > > Given this test code:
    > > .h file
    > > --------------
    > > #include <iostream>
    > > #include <string>
    > > class CContainer {
    > > public:
    > > typedef std::string::size_type size_type;
    > > static const size_type npos2 = std::string::npos;
    > > };
    > > class CTest {
    > > public:
    > > void Foo(CContainer::size_type to = CContainer::npos2);
    > > };
    > >
    > > .cpp file
    > > ---------------
    > > #include "TestApp.h"
    > > using namespace std;
    > > int main()
    > > {
    > > CTest ct;
    > > ct.Foo();
    > > return 0;
    > > }
    > > void CTest::Foo(CContainer::size_type to /* = CContainer::npos2 */)
    > > {
    > > cout << "To == " << to << endl;
    > > cout << "NPOS == " << string::npos << endl;
    > > }
    > >
    > > My output is:
    > > To == 0
    > > NPOS == 4294967295
    > >
    > > Why is To not equal to NPOS????? This snippet is from a larger chunk of
    > > code that was originally compiled on solaris, and in porting it to windows
    > > To is not initialized as I think it should be. Dont have a sun machine to
    > > test it on though.
    > >
    > > Any one have any suggestions on what is going on? Easy to work around, but
    > > the behavior is incorrect as far as I can tell. This was compiled under
    > > .NET 2003 btw.
    > >

    >
    > I've no idea if this would help, and I can't run MSVC to test this idea out,
    > but I would still provide a definition for npos2 to make the program
    > perfectly valid:
    >
    > const CContainer::size_type CContainer::npos2;
    >
    > Denis


    All right, I've just had an opportunity to test it on MS Windows with VC 7.1.
    Your original version worked as desired when compiled with STLport.
    (though without a definition, it is still broken).
    With Dinkumware (the default library configuration for VC, AFAIK), the
    results were the same as you experienced. When I provided a definition as
    shown above, it didn't compile. IIRC, Dinkumware's implementation (at least
    the one I saw), unlike that of STLport, puts the initialiser for string::npos
    outside of the class definition; I think that is the important difference.
    Flipping the initialiser seemed to have solved the problem:

    class CContainer {
    public:
    typedef std::string::size_type size_type;
    static const size_type npos2;
    };

    //...
    //definition (qualify with static if you put it in a header):
    const CContainer::size_type CContainer::npos2 = std::string::npos;

    I don't think I would use this "solution" though. For one thing, it
    doesn't feel right, for another, 9.4.2/4 appears to imply that npos2 cannot
    be used in integral constant expressions unless its initialiser is located
    inside the class definition.


    Denis
     
    Denis Remezov, Jun 8, 2004
    #4
  5. Denis Remezov

    BCC Guest

    "Xiaobin Yang" <> wrote in message
    news:p...
    > > My output is:
    > > To == 0
    > > NPOS == 4294967295
    > >
    > > Why is To not equal to NPOS????? This snippet is from a larger chunk of

    >
    > on my machine, I get:
    > To == 4294967295
    > NPOS == 4294967295
    >


    What kind of machine?
     
    BCC, Jun 8, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen F Zelonis

    Strange Behavior on Default Web page

    Stephen F Zelonis, Apr 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    332
    Stephen F Zelonis
    Apr 5, 2004
  2. nagy
    Replies:
    36
    Views:
    1,066
    Terry Reedy
    Jul 20, 2006
  3. Chris
    Replies:
    34
    Views:
    1,609
  4. Joey
    Replies:
    3
    Views:
    466
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
    Jun 28, 2007
  5. puzzlecracker
    Replies:
    8
    Views:
    455
    James Kanze
    Apr 15, 2008
Loading...

Share This Page