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

Aliases

58 views
Skip to first unread message

Doug Mika

unread,
May 20, 2015, 3:02:56 PM5/20/15
to
Hi, could someone describe in detail (line for line) what the following code does: I found it in a book, and it's not explained clearly, if at all.

(In particular, I don't understand "typename C::value_type" using the scope resolution operator on a template parameter. and should it be
"Container<Element_type>" instead? Also, I should mention that Vector is a custom vector class created by the author.)

template<typename C>
using Element_type = typename C::value_type;

template<typename Container>
void algo(Container& c) {
Vector<Element_type<Container>> vec; //keep results here
//...
}

where Vector is:

template<typename T>
class Vector {
public:
using value_type = T;
// ...
};

Thanks to all and I know I didn't include the code for the custom Vector class, but I hope the above will suffice to give someone an idea.

Wouter van Ooijen

unread,
May 20, 2015, 3:09:11 PM5/20/15
to
Doug Mika schreef op 20-May-15 om 9:02 PM:
> Hi, could someone describe in detail (line for line) what the following code does: I found it in a book, and it's not explained clearly, if at all.
>
> (In particular, I don't understand "typename C::value_type" using the scope resolution operator on a template parameter.
> . . .
>
> template<typename C>
> using Element_type = typename C::value_type;

The scope resolution is used because the author wants to use something
that is declared within the class C. Not a per-object thing like a
normal method or an attribute, but something per-class, like a static
method or a nested classd. Hence :: instead of .

Now the compiler knows that C::value_type is something within the class
C, but it does not know what it is (a class? a method? ...). Hence the
typename prefix to tell the compiler that is is a class.

Wouter



Victor Bazarov

unread,
May 20, 2015, 3:17:47 PM5/20/15
to
On 5/20/2015 3:02 PM, Doug Mika wrote:
> Hi, could someone describe in detail (line for line) what the
> following code does: I found it in a book, and it's not explained
> clearly, if at all.
>
> (In particular, I don't understand "typename C::value_type" using
> the scope resolution operator on a template parameter. and should it
> be "Container<Element_type>" instead? Also, I should mention that
> Vector is a custom vector class created by the author.)
>
> template<typename C>
> using Element_type = typename C::value_type;

This declares 'Element_type' as a template-id (similar to 'typedef-id'
that you could declare using "typedef") that can be used with a single
argument, as if you could any other template, except that the compiler
should immediately upon encountering it look it up and instead use as a
typename the template argument's member 'value_type'. Example: if you
in your code use 'Element_type<Blah>', the compiler should find the
member of 'Blah' called "value_type" and use that instead.

struct Foo {
typedef int value_type; // here is the member
};

int main() {
Element_type<Foo> i; // declares 'i' as 'int'
}

> template<typename Container>
> void algo(Container& c) {
> Vector<Element_type<Container>> vec; //keep results here

Here the compiler should look up the member "value_type" in whatever the
actual type 'Container' is, and substitute it to make the line read

Vector<that> vec;

where "that" is supposed to be the type declared as a member of
'Container' (whatever that is for this specific instantiation of the
'algo' template) under the name 'value_type'.

> //...
> }
>
> where Vector is:
>
> template<typename T>
> class Vector {
> public:
> using value_type = T;
> // ...
> };
>
> Thanks to all and I know I didn't include the code for the custom
> Vector class, but I hope the above will suffice to give someone an
> idea.

If you want to learn more about the resulting types, you should start
using 'typeid' operator in your test code:

std::cout << "Type of 'whatIwant2know' is " <<
typeid(whatIwant2know).name() << std::endl;

Don't forget to include <typeinfo> header.

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

Doug Mika

unread,
May 20, 2015, 3:54:44 PM5/20/15
to
Ok, I think that was fairly clear and well explained, thanks, but I had a quick question. Could I use a particular instance instead of type?
ie
vector<int> vi(1)={1};
Element_type<vi> i; //would this be identical to int i;? or how about this:
Element_type<vector<int>> i;//int i; ?

Victor Bazarov

unread,
May 20, 2015, 9:36:47 PM5/20/15
to
This should not compile - 'Element_type' template needs a *type* as its
argument, and 'vi' is NOT a type. How many times do I need to repeat this?

> Element_type<vector<int>> i;//int i; ?

Yes, this ought to work.

Why don't you just try it? Afraid or something?

Doug Mika

unread,
May 21, 2015, 12:37:37 AM5/21/15
to
I hate to admit it, but I don't have my compiler installed on the machine I was on...sorry

Victor Bazarov

unread,
May 21, 2015, 7:46:31 AM5/21/15
to
There is always http://www.tutorialspoint.com/compile_cpp_online.php if
you have access to web...

Doug Mika

unread,
May 22, 2015, 4:14:39 PM5/22/15
to
That's pretty neat :-)
0 new messages