This question already has answers here:
Const temporary from template type and why use std::add_const?

(2个答案)


已关闭8年。




示例代码取自:http://en.cppreference.com/w/cpp/types/add_cv
(我做了一点修改。)
struct foo
{
    void m() { std::cout << "Non-cv\n"; }
    void m() const { std::cout << "Const\n"; }
};

template<class T>
void call_m()
{
  T().m();
}

int main()
{
    call_m<foo>();
    call_m<const foo>(); //here
}

输出为:
Non-cv
Non-cv

在第二个调用中,T是const限定字符,因此T()应该调用const版本,对吗?还是我错过了一些特殊的规则?

最佳答案

该标准的相关报价为5.2.3 [expr.type.conv]/2



标准中的措词明确提到(以非规范形式),对于非类类型,将删除const volatile资格,但是在您的情况下,该类型为,并且该注释不适用。似乎VS正在应用与非类类型相同的规则。

10-08 08:34
查看更多