C++11 decltype requires instantiated object

Posted by snipes83 on Stack Overflow See other posts from Stack Overflow or by snipes83
Published on 2012-03-18T17:12:30Z Indexed on 2012/03/18 17:55 UTC
Read the original article Hit count: 99

Filed under:
|

I was experimenting a little with the C++11 standard and came up with this problem:

In C++11 you can use auto and decltype to automatically get return type for a function as, for example the begin() and end() functions below:

#include <vector>

template <typename T>
class Container {
private:
    std::vector<T> v;
public:
    auto begin() -> decltype(v.begin()) { return v.begin(); };
    auto end() -> decltype(v.end()) { return v.end(); };
};

My problem here is that I have to declare the private vector<T> v before the public declarations which is against my coding style. I would like to declare all my private members after my public members. You have to declare the vector before the function declaration because the expression in decltype is a call to vector member function begin() and requires an instance of the object.

Is there a way around this?

© Stack Overflow or respective owner

Related posts about coding-style

Related posts about c++11