Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Strange behavior with default function assignment

Reply
Thread Tools

Strange behavior with default function assignment

 
 
Denis Remezov
Guest
Posts: n/a
 
      06-07-2004
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
 
Reply With Quote
 
 
 
 
BCC
Guest
Posts: n/a
 
      06-07-2004
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


 
Reply With Quote
 
 
 
 
Xiaobin Yang
Guest
Posts: n/a
 
      06-08-2004
> 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

 
Reply With Quote
 
Denis Remezov
Guest
Posts: n/a
 
      06-08-2004
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
 
Reply With Quote
 
BCC
Guest
Posts: n/a
 
      06-08-2004

"Xiaobin Yang" <(E-Mail Removed)> wrote in message
news(E-Mail Removed) t.kuleuven.ac.be...
> > 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
Strange Behavior for Pages Named Default.aspx Joey ASP .Net 3 06-28-2007 08:28 AM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
Strange Behavior on Default Web page Stephen F Zelonis ASP .Net 0 04-05-2004 10:49 PM



Advertisments