Questions tagged «decltype»

2
decltype(auto)有什么用?
Наэтотвопросестьответына 堆栈溢出нарусском:Конструкцияdecltype(自动) 在c ++ 14中,decltype(auto)引入了惯用语。 通常,它的用途是允许auto声明使用decltype给定表达式上的规则。 在搜索“良好”使用该用法的示例时,我只能想到以下内容(由Scott Meyers撰写),即函数的返回类型推导: template<typename ContainerType, typename IndexType> // C++14 decltype(auto) grab(ContainerType&& container, IndexType&& index) { authenticateUser(); return std::forward<ContainerType>(container)[std::forward<IndexType>(index)]; } 还有其他使用此新语言功能的示例吗?

2
功能标题中的箭头运算符(->)
我遇到以下代码: template <typename T, typename T1> auto compose(T a, T1 b) -> decltype(a + b) { return a+b; } 我无法理解的一件事是: 在哪里可以找到箭头->标题()在函数标题中的含义?我完全从逻辑上猜测,->运算符确定auto要推导的类型,但是我想弄清楚这一点。我找不到任何信息。
128 c++  c++11  auto  decltype 

2
std :: result_of和decltype之间的区别
我在理解std::result_ofC ++ 0x 的需求时遇到了一些麻烦。如果我理解正确,result_of它用于获得使用某些类型的参数调用函数对象的结果类型。例如: template <typename F, typename Arg> typename std::result_of<F(Arg)>::type invoke(F f, Arg a) { return f(a); } 我看不出以下代码有什么区别: template <typename F, typename Arg> auto invoke(F f, Arg a) -> decltype(f(a)) //uses the f parameter { return f(a); } 要么 template <typename F, typename Arg> auto invoke(F f, Arg …
100 c++  c++11  decltype  result-of 

6
为什么在C ++中引用不是“ const”?
我们知道,“常量变量”表示一旦分配,就不能更改该变量,如下所示: int const i = 1; i = 2; 上面的程序无法编译;gcc提示错误: assignment of read-only variable 'i' 没问题,我可以理解,但是以下示例超出了我的理解范围: #include<iostream> using namespace std; int main() { boolalpha(cout); int const i = 1; cout << is_const<decltype(i)>::value << endl; int const &ri = i; cout << is_const<decltype(ri)>::value << endl; return 0; } 它输出 true false …

3
C ++十进制类型和括号-为什么?
之前已经讨论 过该主题,但这不是重复的。 当有人询问decltype(a)和之间的区别时decltype((a)),通常的答案是- a是变量,(a)是表达式。我觉得这个答案不令人满意。 首先,a也是一个表达。主表达式的选项包括: (表情) id表达 更重要的是,decltype的措词非常非常明确地考虑了括号: For an expression e, the type denoted by decltype(e) is defined as follows: (1.1) if e is an unparenthesized id-expression naming a structured binding, ... (1.2) otherwise, if e is an unparenthesized id-expression naming a non-type template-parameter, ... (1.3) otherwise, if e …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.