473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Strange behavior with default function assignment

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
Jul 22 '05 #1
4 1830
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
Jul 22 '05 #2
> 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

Jul 22 '05 #3
BCC

"Xiaobin Yang" <xy***@esat.kuleuven.ac.be> wrote in message
news:Pi******************************@speyburn.esa 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?
Jul 22 '05 #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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: brian.bird | last post by:
Can anyone explain the behaviour of python when running this script? >>> def method(n, bits=): .... bits.append(n) .... print bits .... >>> method(1) >>> method(2)
4
by: DaKoadMunky | last post by:
<CODE> #include <iostream> using namespace std; int Foo(int x,int y) { int result = x; result*=y; result+=y;
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
1
by: Maileen | last post by:
Hi, I finished my application butonce again i have some strange behavior with XML/text functions... for example, here below is a function which worked perfectly till now and now generate an...
44
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
6
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
28
by: charlie | last post by:
Hi, I found an article on informit.com that talks about C++ casts http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=285&rl=1 The strange thing is the author says the following...
0
by: Antonio Paglia | last post by:
Hi guys, I saw an strange behavior with some printers of series HP Laserjet 1000. I have installed several printers on my PC, some of them are : HP LaserJet 1020 e 1010. I'm using...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.