Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

c++ - Behavior of decltype

Say I have an object of some of stl container classes obj. I can define other object of same type this way:

decltype(obj) obj2;

But I can't declare iterator for the container this way:

decltype(obj)::iterator it = obj.begin();

Why? Am I doing something wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code is well-formed according to the final C++0x draft (FDIS). This was a late change that's not yet been implemented by the Visual Studio compiler.

In the meantime, a workaround is to use a typedef:

typedef decltype(obj) obj_type;
obj_type::iterator it = obj.begin();

EDIT: The relevant chapter and verse is 5.1.1/8:

qualified-id:
    [...]
    nested-name-specifier template
opt unqualified-id nested-name-specifier: [...] decltype-specifier :: decltype-specifier: decltype ( expression )

And for completeness's sake:

The original core issue

Proposal for wording


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...