Take the 2-minute tour ×

I know about value_type, key_type... but they operate on types, not on instances. I tried stuff like :

std::set<uint64_t> mySet;   

decltype (mySet)::value_type pos;

But it doesnt work.

EDIT: I use VS 2010.

EDIT2: the prupose of this code was to get a type to give it to boost::lexical_cast<> is there a workaround that enables that ? I want something like this:

  // it is a iterator in vector of strings

EDIT3 : this works:

share|improve this question
What does "doesn't work" mean? –  Code-Guru Sep 12 '12 at 15:41
decltype (mySet)::value_type pos; is correct. There's a bug in the 2010 Microsoft compiler that doesn't allow decltype to be followed by a :: (see question linked as duplicate) –  Joe Gauterin Sep 12 '12 at 15:42
possible duplicate of decltype and the scope operator in C++ –  Joe Gauterin Sep 12 '12 at 15:42
Given that you're apparently dealing with a buggy compiler, one possible workaround would be something like: auto pos = *(mySet.begin()); –  Jerry Coffin Sep 12 '12 at 15:43
A workaround: typedef decltype(mySet) T; T::value_type pos; –  jrok Sep 12 '12 at 15:45
show 7 more comments

2 Answers

up vote 5 down vote accepted

decltype (mySet)::value_type is correct. Make sure you have C++11 mode enabled in your compiler. If you have, then it's a compiler bug.

A possible workaround involves using the identity metafunction:

template <typename T>
struct identity { typedef T type; };

identity<decltype(mySet)>::type::value_type pos;
share|improve this answer
ive updated my Q –  NoSenseEtAl Sep 12 '12 at 15:48
@NoSenseEtAl VS2010 doesn't let you use scope resolution on decltype, but here's a slightly evil way that makes VS2010's decltype correct: #define decltype(...) std::identity<decltype(__VA_ARGS__)>::type –  Dave Sep 12 '12 at 16:43
add comment

I'd do it the other way around:

typedef std::set<uint_least64_t> set_type;
set_type mySet;
set_type::value_type pos;
share|improve this answer
add comment

Your Answer


