Strange behavior with default function assignment

D

Denis Remezov

BCC said:
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
 
B

BCC

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
 
X

Xiaobin Yang

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
 
D

Denis Remezov

Denis said:
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
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top