Vek*_*ksi 3 c++ visual-c++ c++11 visual-studio-2012
是否可以编写以下代码?我想要做的是do_vector_action可以自动推导出函数的正确返回类型(我实际拥有的代码在cpp文件中定义了函数,而不是在这里的头文件中).
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;
}
}
Run Code Online (Sandbox Code Playgroud)
而且,我也想知道,是否可以替换typedef,如
class some_class
{
public:
typedef std::vector<int> int_vector_type;
int_vector_type int_vector;
int_vector_type::size_type size;
}
Run Code Online (Sandbox Code Playgroud)
使用decltype或其他一些构造如
class some_class
{
public:
std::vector<int> int_vector;
decltype(int_vector)::size_type size;
}
Run Code Online (Sandbox Code Playgroud)
因为带有decltype的最后一个片段无法使用Visual Studio 2012 RC进行编译.
decltype(int_vector.size()) something + 1;
Run Code Online (Sandbox Code Playgroud)
这相当于:
std::vector<int>::size_type something + 1;
Run Code Online (Sandbox Code Playgroud)
这是不正确的(你声明一个名为的变量something......添加一个?
你的第二个例子,使用decltype(int_vector)::size_type是有效的.由于编译器错误(*), Visual C++ 2010和2012拒绝它.作为解决方法,您应该能够声明size为:
identity<decltype(int_vector)>::type::size_type size;
Run Code Online (Sandbox Code Playgroud)
假设存在标准identity模板,声明为:
template <typename T>
struct identity { typedef T type; };
Run Code Online (Sandbox Code Playgroud)
(*)decltype在嵌套名称说明符中使用的能力在C++ 11标准化过程的最后添加(参见N3031 [PDF]).这是在Visual C++ 2010完成之后,并且在Visual C++ 2012中未添加对此添加的支持.
| 归档时间: |
|
| 查看次数: |
840 次 |
| 最近记录: |