class scope typedef through dot operator

J

Jonathan Lee

So I think I've seen somewhere that you can access a class-scoped
typedef via the dot operator, like other members. For example,

std::vector<double> v;
/* do something with v.value_type */

The thing is, I can't seem to actually do anything with it. Can't take
the size of it, can't construct a variable, etc. But the compiler
errors suggest it's actually figuring out that I *mean*
std::vector<double>::value_type. What can you actually accomplish with
this notation? Or is it even valid?

--Jonathan
 
J

Joshua Maurice

So I think I've seen somewhere that you can access a class-scoped
typedef via the dot operator, like other members. For example,

  std::vector<double> v;
  /* do something with v.value_type */

The thing is, I can't seem to actually do anything with it. Can't take
the size of it, can't construct a variable, etc. But the compiler
errors suggest it's actually figuring out that I *mean*
std::vector<double>::value_type. What can you actually accomplish with
this notation? Or is it even valid?

value_type is a type name, not a data member or function member. The
dot operator is used to access data members and function members. The
scope resolution operator "::" is used for things like nested type
names. The compiler is telling you exactly that. The syntax to use
value_type is "std::vector<double>::value_type". Ex:
#include <vector>
std::vector<double>::value_type some_variable;

What exactly is your question?
 
J

Jonathan Lee

value_type is a type name, not a data member or function member. The
dot operator is used to access data members and function members. The
scope resolution operator "::" is used for things like nested type
names. The compiler is telling you exactly that. The syntax to use
value_type is "std::vector<double>::value_type". Ex:
  #include <vector>
  std::vector<double>::value_type some_variable;

What exactly is your question?

Sorry, I misremembered the example. I thought it was for typedefs,
but it was actually for enums. i.e,

class A {
public:
enum dir { left, right };
};

int main() {
A a;
A* b = &a;

std::cout << static_cast<int>(a.left) << std::endl;
std::cout << static_cast<int>(b->left) << std::endl;
}

So I guess I don't have a question after all.
--Jonathan
 
R

red floyd

Sorry, I misremembered the example. I thought it was for typedefs,
but it was actually for enums. i.e,

class A {
public:
enum dir { left, right };
};

int main() {
A a;
A* b =&a;

std::cout<< static_cast<int>(a.left)<< std::endl;
std::cout<< static_cast<int>(b->left)<< std::endl;
}

left and right should still be accessed using the scope operator:

A::left, A::right
 
J

Juha Nieminen

red floyd said:
left and right should still be accessed using the scope operator:

A::left, A::right

Why? What difference does it make? (Which, I think, was the original
question.)
 
V

Victor Bazarov

Why? What difference does it make? (Which, I think, was the original
question.)

Historical reasons, I would guess. Makes things easier to look up,
maybe. Makes the code more readable as well, although some might want
to debate that (but that's because they want to debate, and not because
it's debatable).

V
 
B

Bo Persson

Joshua said:
value_type is a type name, not a data member or function member. The
dot operator is used to access data members and function members.
The scope resolution operator "::" is used for things like nested
type names.

Yes, but the dot operator very well could have been used to access
nested types of a variable (or its type actually). It would have been
very convenient at times to write v.value_type or v.iterator_type,
without knowing the exact type of v.

The compiler can obviously figure out that it's intended, even though
the syntax doesn't allow it.


Bo Persson
 

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

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
alexanderrm

Latest Threads

Top