mercredi 2 octobre 2019

C++ - struggling to understand how decltype() evaluates member access expressions

I have been trying to figure out when and why the decltype() yeilds const but I don't get any in-depth details about it.

struct  S
{
    double d;
    double& dr = d;
    double* dPtr;
    vector<double> vec;
};

const S cs;

decltype(cs.d) d; // yeilds  double
decltype((cs.d)) dd; // yeilds  const double&
decltype(*cs.dPtr) deRef; // yeilds  double&
decltype(cs.dr) ref; // yeilds  double&
decltype(cs.vec.begin()) it; // yeilds  const_iterator

I thought every lvaule reference that decltype() yeilds will be const due to the const specifier before S but I was wrong. How does decltype() determine a type is const or nonconst when the expressions are member access?

It's really difficult for me to understand the difference between the result from decltype((cs.d)) and decltype(cs.dr) (also the decltype(*cs.dPtr)). And as we can see, iterator will be automatically converted to const_iterator. Are there any rules for decltype() with the const specifier?

Aucun commentaire:

Enregistrer un commentaire