Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

getting at value_type?

29 views
Skip to first unread message

Doug Mika

unread,
Jun 16, 2015, 5:13:36 PM6/16/15
to
Why can't I do the following:
#include <iostream>
#include <string>

using namespace std;

template<typename T>
struct C {
using value_type=T;
string name;
int number;
};

int main()
{
C<int> c1{"Michael", 1};
C<string> c2{"Suzanne",2};

c1.value_type x; //Line xx - doesn't work

return 0;
}

Instead what I must do in line xx is decltype(c1)::value_type x;?
Is value_type a member of c1? (just as is name)

Victor Bazarov

unread,
Jun 16, 2015, 5:23:18 PM6/16/15
to
It's a member of the class, not a member of an object.

V
--
I do not respond to top-posted replies, please don't ask

Doug Mika

unread,
Jun 16, 2015, 5:45:46 PM6/16/15
to
In that case wondering whether you'd know:

on the reference site, query vector:
http://www.cplusplus.com/reference/vector/vector/?kw=vector

when you scroll down it gives a list of Member Types. How can I know whether they are object members or class members?

Paavo Helde

unread,
Jun 16, 2015, 5:56:00 PM6/16/15
to
Victor Bazarov <v.ba...@comcast.invalid> wrote in news:mlq41d$2n3$2
@dont-email.me:

> On 6/16/2015 5:13 PM, Doug Mika wrote:
>> Why can't I do the following:
>> #include <iostream>
>> #include <string>
>>
>> using namespace std;
>>
>> template<typename T>
>> struct C {
>> using value_type=T;
>> string name;
>> int number;
>> };
>>
>> int main()
>> {
>> C<int> c1{"Michael", 1};
>> C<string> c2{"Suzanne",2};
>>
>> c1.value_type x; //Line xx - doesn't work
>>
>> return 0;
>> }
>>
>> Instead what I must do in line xx is decltype(c1)::value_type x;?
>> Is value_type a member of c1? (just as is name)
>
> It's a member of the class, not a member of an object.

Static member functions are also members of the class, yet can be called
on an object. Just saying...

Cheers
Paavo

Victor Bazarov

unread,
Jun 16, 2015, 6:07:11 PM6/16/15
to
By RTFMing, maybe?

Victor Bazarov

unread,
Jun 16, 2015, 6:14:13 PM6/16/15
to
Yes, why don't we just unify anything that sounds similar? I guess it
was too much to ask of the compiler creators to allow types to be
figured out from an expression, like in a declaration:

(object1 @ object2).sometypename variablename;

(here 'object1 @ object2' is an expression that yields another object,
of some type. The Standard writers agreed that to simplify the parsing
(again, I'm guessing here) you need to write

decltype(...) :: sometypename ...

since 'decltype' yields a *type*, and not an instance, you need to use
the scope resolution operator to get to a member type.

Richard

unread,
Jun 18, 2015, 12:24:56 AM6/18/15
to
[Please do not mail me a copy of your followup]

Doug Mika <doug...@gmail.com> spake the secret code
<8e40adf2-6161-436b...@googlegroups.com> thusly:

>Why can't I do the following:

Because nested type declarations in an object are not members of an
instance of an object.
c1 is an instance of a class, it is not a type. You declare variables
using a type. You can't say 'c1.value_type x' any more than you can
say 'c1 x;'.

C<int> is the type of c1. C<int>::value_type is the nested type.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
0 new messages