使用像typedef这样的decltype
Using decltype like typedef
尝试在必要时使用decltype
代替typedef
或auto
关键字。其中一些(stl和用户函数(工作,但它不能直接与用户定义的对象一起工作。请我在这里需要解释。
#include <iostream>
#include <vector>
using namespace std;
struct foo {
void operator=(const int a ) { val_ =a;}
int get() const { return val_;}
private:
int val_;
};
ostream& operator << (ostream& os, foo f )
{
os << f.get();
return os;
}
foo func()
{
foo a;
return a;
}
int main()
{
vector<foo> foos;
decltype(foos) b; //works
decltype(foo);// don't work - says - error C3553: decltype expects an expression not a type
decltype(func()) a; //works
a = 100;
std::cout << a << " n";
}
编辑
vector<foo> foos;
decltype(foos) b; //works :tested with vc++ compiler
decltype(vector<foo>) b; //fails
>decltype
等效于其对象操作数(或虚拟对象(的声明类型。因此,您向它提供一个对象并在编译时获取该类。由于结果是一个类型,因此可以在任何需要typename
的地方使用它。你不能用类型来喂它。 https://en.cppreference.com/w/cpp/language/decltype
相关文章:
- C++:TypeDef使用元组
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- C++decltype和圆括号-为什么
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 如何从"decltype()"获取函数参数的数量<funtion>?
- Confusion: decltype vs std::function
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 为什么在使用typedef时类推导指南会失败
- decltype(1, t) 应该是 l 值引用吗?(编译器不同意)
- 尝试根据类中 typedef 的存在来专门化模板函数
- 如何重新定义MPI_FLOAT,MPI_DOUBLE以 typedef 的方式
- typedef 枚举和枚举类有什么区别?
- 在类模板中使用 typedef 时出错
- 模板类中的 typedef 语句
- 在 typedef 内部使用 const 关键字和在 typedef 外部使用 const 关键字之间有区别吗?
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 是否可以在不使用 decltype 的情况下推断先前定义的 extern 变量的类型
- 无效 f(int) 的模板和 decltype
- c++ 11 带有 decltype 的尾随返回类型无法按预期工作
- 使用像typedef这样的decltype