Just Curious....... Why no :: for objects?

  • Follow


I'm just curious about why you can't do the following:

    vector<int> vec;

    for ( vec::iterator it = vec.begin(); it != vec.end; ++it )
    {

    }

Would it not be handy to be able to use :: on objects?
0
Reply virtual (44) 5/15/2011 6:47:40 PM

Virchanza  wrote:

> 
> I'm just curious about why you can't do the following:
> 
>     vector<int> vec;
> 
>     for ( vec::iterator it = vec.begin(); it != vec.end; ++it )
>     {
> 
>     }
> 
> Would it not be handy to be able to use :: on objects?

I don't think it would have caused too much problem if it had been
allowed (although that would require way more thinking), but in most
cases, when you have an element of some type, you also have the given
type handy (here vector<int>), and in the other cases... well soon
enough you'll have decltype.
0
Reply marc.glisse (238) 5/15/2011 7:37:37 PM


On 15/05/2011 19:47, Virchanza wrote:
>
> I'm just curious about why you can't do the following:
>
>      vector<int>  vec;
>
>      for ( vec::iterator it = vec.begin(); it != vec.end; ++it )
>      {
>
>      }
>
> Would it not be handy to be able to use :: on objects?


For this particular problem if you are using C++0x you can do:

vector<int> vec;
for ( auto it = vec.begin(); it != vec.end(); ++it )
{
}

Until then I usually use a typedef:

typedef vector<int> vec_t;
vec_t vec;
for ( vec_t::iterator = vec.begin(); it != vec.end(); ++it )
{
}

HTH

/Leigh
0
Reply leigh (1003) 5/15/2011 7:38:52 PM

Virchanza wrote:

> 
> I'm just curious about why you can't do the following:
> 
>     vector<int> vec;
> 
>     for ( vec::iterator it = vec.begin(); it != vec.end; ++it )
>     {
> 
>     }
> 
> Would it not be handy to be able to use :: on objects?

There is one problem: "::" ignores non-{class,enumeration,namespace,type-
template} names. So if you were to do "vec::begin()" it would ignore your 
local "vec" variable, and look for a vec in a surrounding scope. 

On those grounds, I'm not sure it's possible to allow "::" as a replacement 
for ".".
0
Reply schaub.johannes (56) 5/15/2011 9:32:57 PM

* Johannes Schaub, on 15.05.2011 23:32:
> Virchanza wrote:
>
>>
>> I'm just curious about why you can't do the following:
>>
>>      vector<int>  vec;
>>
>>      for ( vec::iterator it = vec.begin(); it != vec.end; ++it )
>>      {
>>
>>      }
>>
>> Would it not be handy to be able to use :: on objects?
>
> There is one problem: "::" ignores non-{class,enumeration,namespace,type-
> template} names. So if you were to do "vec::begin()" it would ignore your
> local "vec" variable, and look for a vec in a surrounding scope.
>
> On those grounds, I'm not sure it's possible to allow "::" as a replacement
> for ".".

For the above the most handy would IMHO be C++0x "auto".


Cheers,

- Alf  "o  o"
          ::

-- 
blog at <url: http://alfps.wordpress.com>
0
Reply usenet30 (677) 5/15/2011 10:52:43 PM

Am 16.05.2011 00:52, schrieb Alf P. Steinbach /Usenet:
> * Johannes Schaub, on 15.05.2011 23:32:
>> Virchanza wrote:
>>
>>>
>>> I'm just curious about why you can't do the following:
>>>
>>> vector<int> vec;
>>>
>>> for ( vec::iterator it = vec.begin(); it != vec.end; ++it )
>>> {
>>>
>>> }
>>>
>>> Would it not be handy to be able to use :: on objects?
>>
>> There is one problem: "::" ignores non-{class,enumeration,namespace,type-
>> template} names. So if you were to do "vec::begin()" it would ignore your
>> local "vec" variable, and look for a vec in a surrounding scope.
>>
>> On those grounds, I'm not sure it's possible to allow "::" as a
>> replacement
>> for ".".
>
> For the above the most handy would IMHO be C++0x "auto".

Or the new C++0x for loop style:

for (int i : vec)
{}

-- 
Thomas
0
Reply Phygon_ANTISPAM (698) 5/16/2011 1:11:43 AM

Johannes Schaub  wrote:

> Virchanza wrote:
> 
>> 
>> I'm just curious about why you can't do the following:
>> 
>>     vector<int> vec;
>> 
>>     for ( vec::iterator it = vec.begin(); it != vec.end; ++it )
>>     {
>> 
>>     }
>> 
>> Would it not be handy to be able to use :: on objects?
> 
> There is one problem: "::" ignores non-{class,enumeration,namespace,type-
> template} names. So if you were to do "vec::begin()" it would ignore your 
> local "vec" variable, and look for a vec in a surrounding scope. 

He's talking of having different rules, that implies not applying the
current rules...

> On those grounds, I'm not sure it's possible to allow "::" as a replacement 
> for ".".

Er, you seem to have misread the post (or I did). It looks like he
doesn't want :: to mean . but :: to implicitly apply decltype to its
left if it is not a type already.
0
Reply marc.glisse (238) 5/16/2011 9:04:52 AM

On 5/15/2011 11:47 AM, Virchanza wrote:
>
> I'm just curious about why you can't do the following:
>
>      vector<int>  vec;
>
>      for ( vec::iterator it = vec.begin(); it != vec.end; ++it )
>      {
>
>      }
>
> Would it not be handy to be able to use :: on objects?

Well, how long have you been a developer?

Any time you're forced to develop something that deals with some bit of 
information that can mean multiple different things, depending on 
context, you simply make the problem more difficult to solve.  Parsers 
would get more complicated, I don't know by how much.  So my guess is 
that they didn't do this because it was too much work for too little gain.

I wish I could do that.

-- 
http://crazycpp.wordpress.com
0
Reply dont78 (249) 5/16/2011 4:32:00 PM

7 Replies
29 Views

(page loaded in 0.12 seconds)


Reply: