:: with object

  • Thread starter Marcin Kalicinski
  • Start date
M

Marcin Kalicinski

Hi,

map<string, string> m;

(1) map<string, string>::iterator i = m.find("x");
(2) m::iterator i = m.find("x");

Does the extended syntax of :: operator as shown by (2) conflicts with any
existing C++ construct? If possible, it would make things shorter without
all these typedefs.

Best regards,
Marcin
 
M

Martijn Lievaart

Hi,

map<string, string> m;

(1) map<string, string>::iterator i = m.find("x");
(2) m::iterator i = m.find("x");

Does the extended syntax of :: operator as shown by (2) conflicts with any
existing C++ construct? If possible, it would make things shorter without
all these typedefs.

I like it. I've been thinking along the same lines. But you'll ghet better
answers in comp.std.c++.

Note that C++0x will probably include some construct like:

(3) auto i = m.find("x");

Which would be nice as well, even if not completely the same.

M4
 
J

Jeff Schwab

Martijn said:
I like it. I've been thinking along the same lines. But you'll ghet better
answers in comp.std.c++.

Note that C++0x will probably include some construct like:

(3) auto i = m.find("x");

Which would be nice as well, even if not completely the same.

M4


namespace m
{
struct iterator { };
}

#include <map>
#include <string>

int main( )
{
std::map< std::string, std::string > m;
std::map< std::string, std::string >::iterator i = m.find( "x" );
m::iterator j = m.find( "x" );
}
 
M

Marcin Kalicinski

Hi,

I believe that

namespace m { typedef std::map<std::string, std::string>::iterator
iterator; }

will work better. Anyway, it does not solve the problem of iterator
expressions being too long in general. Actually, it makes me need to add a
new namespace for each different iterator expression I write. You can
quickly run out of names :)

Best regards,
Marcin
 
M

Martijn Lievaart

On Mon, 05 Jan 2004 10:09:17 +0100, Marcin Kalicinski wrote:

[ Please don't top post, it makes the thread hard to follow. Thx, M4 ]
I believe that

namespace m { typedef std::map<std::string, std::string>::iterator
iterator; }

will work better. Anyway, it does not solve the problem of iterator
expressions being too long in general. Actually, it makes me need to add a
new namespace for each different iterator expression I write. You can
quickly run out of names :)

What is wrong with typedefs? Getting back to the original problem:

typedef map<string, string> mymap;
mymap m;
mymap::iterator i = m.find("x");

The OP had a valid point, but these namespace tricks are not the answer
IMO.

HTH,
M4
 
J

Jeff Schwab

Martijn said:
On Mon, 05 Jan 2004 10:09:17 +0100, Marcin Kalicinski wrote:

[ Please don't top post, it makes the thread hard to follow. Thx, M4 ]

I believe that

namespace m { typedef std::map<std::string, std::string>::iterator
iterator; }

will work better. Anyway, it does not solve the problem of iterator
expressions being too long in general. Actually, it makes me need to add a
new namespace for each different iterator expression I write. You can
quickly run out of names :)


What is wrong with typedefs? Getting back to the original problem:

typedef map<string, string> mymap;
mymap m;
mymap::iterator i = m.find("x");

The OP had a valid point, but these namespace tricks are not the answer
IMO.

HTH,
M4

I didn't provide any trick, just a case for which m::iterator would not
work as teh OP expected. typedef's are certainly the easiest and most
intuitive way to rename types.
 
T

tom_usenet

Hi,

I believe that

namespace m { typedef std::map<std::string, std::string>::iterator
iterator; }

will work better.

I don't think it was meant to provide a solution, just show that your
feature would clash with a feature C++ already has. Should the code
use m::iterator or map::iterator?

Here's the code again (slightly modified):

namespace m
{
struct iterator { };
}

#include <map>
#include <string>

int main( )
{
std::map< std::string, std::string > m;
std::map< std::string, std::string >::iterator i = m.find("x");
m::iterator j;
}

The code is well-formed in current C++. With your proposed change,
what should the code mean? What is the type of j? Or should it be
ambiguous?

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
M

Martijn Lievaart

I didn't provide any trick, just a case for which m::iterator would not
work as teh OP expected. typedef's are certainly the easiest and most
intuitive way to rename types.

I noticed your point later, sorry about that.

But to answer that point, I think normal lookup rules should apply, so the
local m gets found before the namespace m. So this 'feature' may actually
break existing programs and cannot be implemented as proposed.

M4
 

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

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top