C++ decltype和括号的答案是错误的?

C++ decltype和括号的答案是错误的?,c++,c++11,decltype,const-reference,C++,C++11,Decltype,Const Reference,我读到: 但我不明白答案 如果(a->x)的类型是constdouble&为什么要运行此代码 #include <iostream> struct A { double x; }; int main() { A *a=new A; decltype(a->x) x3; decltype((a->x)) x4 = x3; // is it really const double& ?? x4=3;// no error !

我读到:

但我不明白答案

如果(a->x)的类型是constdouble&为什么要运行此代码

#include <iostream>

struct A { double x; };

int main()
{
    A *a=new A;
    decltype(a->x) x3;
    decltype((a->x)) x4 = x3; // is it really const double& ??
    x4=3;// no error !

    const double& x5=x3;
    x5=5;//error 
}
#包括
结构A{double x;};
int main()
{
A*A=新的A;
decltype(a->x)x3;
decltype((a->x))x4=x3;//它真的是常数double&??
x4=3;//没有错误!
常数双&x5=x3;
x5=5;//错误
}

评论中回答了这个问题。然而,我想教未来的读者如何自己回答这个问题。在此示例中添加一点代码可以使编译器本身告诉您类型:

#include <type_traits>
#include <typeinfo>
#include <iostream>
#ifndef _MSC_VER
#   include <cxxabi.h>
#endif
#include <memory>
#include <string>
#include <cstdlib>

template <typename T>
std::string
type_name()
{
    typedef typename std::remove_reference<T>::type TR;
    std::unique_ptr<char, void(*)(void*)> own
           (
#ifndef _MSC_VER
                abi::__cxa_demangle(typeid(TR).name(), nullptr,
                                           nullptr, nullptr),
#else
                nullptr,
#endif
                std::free
           );
    std::string r = own != nullptr ? own.get() : typeid(TR).name();
    if (std::is_const<TR>::value)
        r += " const";
    if (std::is_volatile<TR>::value)
        r += " volatile";
    if (std::is_lvalue_reference<T>::value)
        r += "&";
    else if (std::is_rvalue_reference<T>::value)
        r += "&&";
    return r;
}

#include <iostream>

struct A { double x; };

int main()
{
    A *a=new A;
    std::cout << "decltype(a->x) has type " <<  type_name<decltype(a->x)>() << '\n';
    std::cout << "decltype((a->x)) has type " <<  type_name<decltype((a->x))>() << '\n';
//     decltype(a->x) x3;
//     decltype((a->x)) x4 = x3; // is it really const double& ??
//     x4=3;// no error !
// 
//     const double& x5=x3;
//     x5=5;//error 
}

评论中回答了这个问题。然而,我想教未来的读者如何自己回答这个问题。在此示例中添加一点代码可以使编译器本身告诉您类型:

#include <type_traits>
#include <typeinfo>
#include <iostream>
#ifndef _MSC_VER
#   include <cxxabi.h>
#endif
#include <memory>
#include <string>
#include <cstdlib>

template <typename T>
std::string
type_name()
{
    typedef typename std::remove_reference<T>::type TR;
    std::unique_ptr<char, void(*)(void*)> own
           (
#ifndef _MSC_VER
                abi::__cxa_demangle(typeid(TR).name(), nullptr,
                                           nullptr, nullptr),
#else
                nullptr,
#endif
                std::free
           );
    std::string r = own != nullptr ? own.get() : typeid(TR).name();
    if (std::is_const<TR>::value)
        r += " const";
    if (std::is_volatile<TR>::value)
        r += " volatile";
    if (std::is_lvalue_reference<T>::value)
        r += "&";
    else if (std::is_rvalue_reference<T>::value)
        r += "&&";
    return r;
}

#include <iostream>

struct A { double x; };

int main()
{
    A *a=new A;
    std::cout << "decltype(a->x) has type " <<  type_name<decltype(a->x)>() << '\n';
    std::cout << "decltype((a->x)) has type " <<  type_name<decltype((a->x))>() << '\n';
//     decltype(a->x) x3;
//     decltype((a->x)) x4 = x3; // is it really const double& ??
//     x4=3;// no error !
// 
//     const double& x5=x3;
//     x5=5;//error 
}

您忘记了
A*A=新A中的
const
在链接的示例中,它是
const A*A=new A()
(a->x)=x3
合法吗?如果是,
decltype((a->x))
double&
@MarkGarcia tnx我知道了,你忘了
a*a=new a中的
常量
在链接的示例中,它是
const A*A=new A()
(a->x)=x3
合法吗?如果是的话,
decltype((a->x))
double&
@MarkGarcia-tnx,我明白了