Should it be possible to write the following code? What I’d like to do is that the do_vector_action could automatically deduce the correct return type of the function (the code I actually have has the function defined in a cpp file, not in the header as here).
class some_class
{
public:
std::vector<int> int_vector;
auto do_vector_action() -> decltype(int_vector_.size())
{
decltype(int_vector.size()) something + 1;
return something;
}
}
Moreover, I’d like also know, would it be possible to replace typedefs such as
class some_class
{
public:
typedef std::vector<int> int_vector_type;
int_vector_type int_vector;
int_vector_type::size_type size;
}
with using decltype or some other construct such as
class some_class
{
public:
std::vector<int> int_vector;
decltype(int_vector)::size_type size;
}
as the last snippet with decltype doesn’t compile with Visual Studio 2012 RC.
This is equivalent to:
which is ill-formed (you are declaring a variable named
somethingthen… adding one to it?Your second example, using
decltype(int_vector)::size_typeis valid. Visual C++ 2010 and 2012 reject it due to a compiler bug(*). As a workaround, you should be able to declaresizeas:assuming the presence of a standard
identitytemplate declared as:(*) The ability to use
decltypein a nested name specifier was added very near the end of the C++11 standardization process (see N3031 [PDF]). This was after Visual C++ 2010 was completed, and support for this addition was not added in Visual C++ 2012.