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