Topic: Namespace-bound typedef


Author: francis@robinton.demon.co.uk (Francis Glassborow)
Date: Wed, 11 Jun 2003 20:46:08 +0000 (UTC)
Raw View
In article <bc6pmj$1kd$1@SunSITE.icm.edu.pl>, Maciej Sobczak
<maciej@maciejsobczak.com> writes
>What do you think about it? Would it be a good extension to the
>language? Would it solve the typedef problem? Or would it create more
>problems than it solves?

I think that this is really just another place where the concept of a
strong typedef would be useful. I would prefer to solve the bigger
problem rather than just part of it.


--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: tslettebo@chello.no.nospam (=?iso-8859-1?Q?Terje_Sletteb=F8?=)
Date: Wed, 11 Jun 2003 20:46:14 +0000 (UTC)
Raw View
"Maciej Sobczak" <maciej@maciejsobczak.com> wrote in message
news:bc6pmj$1kd$1@SunSITE.icm.edu.pl...
>
> I would like to discuss here the idea of "namespace-bound typedef".
> (the following is just copy'n'pasted from my post to c.l.c++.m)
>
>      using typedef std::pair<std::string, int> word_type;
>
> Such a construct would make word_type a new type (not just a new name
> for existing type), identical to pair<...>, with exception that for
the
> sake of ADL it would be bound to MyNS and not to std.

A similar proposition has been made in the form of creating "strong
typedefs", meaning typedefs that are not aliases, but unique types.
There was also an article about that in CUJ a while ago.

Another syntax that has been suggested for this is to use "new" as a
modifier. Something like:

typedef new std::pair<std::string, int> word_type;

However, I think the "using" syntax you propose may be better, as
there's already a similar use of it. On the other hand, a "using"
declaration doesn't currently introduce a new type (or name, in
general); it only makes it accessible.

Note that this is not necessarily a trivial change. typedef is formally
defined as changing a name into a type name. It also means that the
order in which it's used in a declaration doesn't matter (at least
according to the grammar, but compilers, even EDG, disagrees on some of
the orders it may be used). For example, it appears that both the
following are valid according to the standard:

typedef const volatile unsigned long double name;

double long unsigned volatile const typedef name;

:)

If Francis Glassborow's suggestion, of being able to inherit
constructors from a base class, was accepted, then we could have done
like Siemel Naran suggested in that thread, but with a twist:

struct word_type : std::pair<std::string, int> { default
word_type=std::pair<std::string, int>; };

Or make it into a generic construct:

template<class T>
struct strong_typedef : T { default strong_typedef=T; };

Use:

typedef strong_typedef<std::pair<std::string, int> > word_type;

As inspector Closeau might have said it: "And the case is solved." :)

The proposal is here
(http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1445.htm).

See, Francis? I told you it was a good proposal you had. ;)


Regards,

Terje

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: andreytarasevich@hotmail.com (Andrey Tarasevich)
Date: Thu, 12 Jun 2003 00:19:08 +0000 (UTC)
Raw View
Maciej Sobczak wrote:
> ...
> There is an ongoing thread on comp.lang.c++.moderated ("ostream_iterator
> problem"), where the OP stumbled over Koenig Lookup with typedef'ed types.
>
> The problem is:
>
> typedef std::pair<std::string, int> word_type;
>
> std::ostream & operator<<(std::ostream &os, const word_type &w)
> {
>      return os << w.first << ' ' << w.second;
> }
>
> and later:
>
> word_type w = ...
>
> cout << w;
>
> The problem is that custom operator<< will not be found, since both cout
> and w have types from std.
> ...

While the problem you are describing definitely exists, this particular
example does not demonstrate it. Assuming that the typedef-definition and
the custom operator declaration are located in the global namespace and the
expression statement 'cout << w' takes place, say, in function 'int main()',
the compiler _will_ find and use the custom operator. Other examples, which
actually illustrate the problem can be found in the aforementioned tread in
'comp.lang.c++.moderated'.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: maciej@maciejsobczak.com (Maciej Sobczak)
Date: Wed, 11 Jun 2003 18:03:57 +0000 (UTC)
Raw View
Hello,

There is an ongoing thread on comp.lang.c++.moderated ("ostream_iterator
problem"), where the OP stumbled over Koenig Lookup with typedef'ed types.

The problem is:

typedef std::pair<std::string, int> word_type;

std::ostream & operator<<(std::ostream &os, const word_type &w)
{
     return os << w.first << ' ' << w.second;
}

and later:

word_type w = ...

cout << w;

The problem is that custom operator<< will not be found, since both cout
and w have types from std.

The dirty solution is to put operator<< in namespace std, which is not
allowed by the Standard. Another "solution" is to define word_type from
scratch, without any reference to std::pair.

I would like to discuss here the idea of "namespace-bound typedef".
(the following is just copy'n'pasted from my post to c.l.c++.m)

Consider the following (not valid now) code:

namespace MyNS
{
     // note 'using' keyword here
     using typedef std::pair<std::string, int> word_type;

     std::ostream & operator<<(std::ostream &os, const word_type &w)
     {
         return os << w.first << ' ' << w.second;
     }
}

Such a construct would make word_type a new type (not just a new name
for existing type), identical to pair<...>, with exception that for the
sake of ADL it would be bound to MyNS and not to std.
As a result, the following:

cout << w;

would work (the operator<< would be found in MyNS, because it is the
namespace to which word_type is bound).

I think such an extension would add some completeness to ADL. At the
moment thare is a little gap where typedef enters the scene.

What do you think about it? Would it be a good extension to the
language? Would it solve the typedef problem? Or would it create more
problems than it solves?

Thank you for your comments,

--
Maciej Sobczak
http://www.maciejsobczak.com/

Distributed programming lib for C, C++, Python & Tcl:
http://www.maciejsobczak.com/prog/yami/

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]