c++ - auto 和 decltype 的关系

标签 c++ c++11 auto type-inference decltype

auto x = initializer;

相当于

decltype(initializer) x = initializer;

decltype((initializer)) x = initializer;

或者两者都没有?

最佳答案

decltype 还考虑表达式是 rvalue 还是 lvalue

Wikipedia says ,

The type denoted by decltype can be different from the type deduced by auto.

#include <vector>
int main()
{
    const std::vector<int> v(1);
    auto a = v[0];        // a has type int
    decltype(v[0]) b = 1; // b has type const int&, the return type of
                        // std::vector<int>::operator[](size_type) const
    auto c = 0;           // c has type int
    auto d = c;           // d has type int
    decltype(c) e;        // e has type int, the type of the entity named by c
    decltype((c)) f = c;  // f has type int&, because (c) is an lvalue
    decltype(0) g;        // g has type int, because 0 is an rvalue
}

这几乎解释了重要的区别。注意 decltype(c)decltype((c)) 不一样!

有时 autodecltype 以合作的方式一起 工作,例如在以下示例中(取自 wiki 和稍作修改):

int& foo(int& i);
float foo(float& f);

template <class T>
auto f(T& t) −> decltype(foo(t)) 
{
  return foo(t);
}

维基百科进一步explains the semantics decltype 如下:

Similarly to the sizeof operator, the operand of decltype is unevaluated. Informally, the type returned by decltype(e) is deduced as follows:

  • If the expression e refers to a variable in local or namespace scope, a static member variable or a function parameter, then the result is that variable's or parameter's declared type
  • If e is a function call or an overloaded operator invocation, decltype(e) denotes the declared return type of that function
  • Otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e; if e is an rvalue, the result is T

These semantics were designed to fulfill the needs of generic library writers, while at the same time being intuitive for novice programmers, because the return type of decltype always matches the type of the object or function exactly as declared in the source code. More formally, Rule 1 applies to unparenthesized id-expressions and class member access expressions. For function calls, the deduced type is the return type of the statically chosen function, as determined by the rules for overload resolution. Example:

const int&& foo();
int i;
struct A { double x; };
const A* a = new A();
decltype(foo()) x1; // type is const int&&
decltype(i) x2; // type is int
decltype(a->x) x3; // type is double
decltype((a->x)) x4; // type is const double&

The reason for the difference between the latter two invocations of decltype is that the parenthesized expression (a->x) is neither an id-expression nor a member access expression, and therefore does not denote a named object.Because the expression is an lvalue, its deduced type is "reference to the type of the expression", or const double&.

关于c++ - auto 和 decltype 的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6869888/

相关文章:

c++ - std::vector<>::emplace_back() 中的异常安全吗?

c++ - 如何让这个音频延迟工作?

c++ - 无法将‘void (myClass::*)() 转换为 void (*)()

c++ - ./libmylib.so : undefined reference to `submarinex::LIB::kCount'

c++ - 使用 decltype(auto) 的基于范围的 for 循环

c++ - auto 在 C++ 中做的事情是否与在 C 中完全不同?

c++ - 如何从二进制文件安装 memcached 并包含在 c 文件中

c++ - 在包含 vector<bool> 的类中重新定义运算符 []

c++ - SFINAE 离开了一个拷贝构造函数

c++ - C++17模板参数中auto的优点