是什么const在"顶级"预选赛中,C平均++?
还有什么其他水平?
例如:
int const *i;
int *const i;
int const *const i;
#include <iostream>
using namespace std;
template<typename T>
void f(T&&) { cout << "f(T&&)" << endl; }
template<typename T>
void f(const T&&) { cout << "f(const T&&)" << endl; }
struct A {};
const A g1() { return {}; }
const int g2() { return {}; }
int main()
{
    f(g1()); // outputs "f(const T&&)" as expected.
    f(g2()); // outputs "f(T&&)" not as expected.
}
问题描述嵌入在代码中.我的编译器是clang 5.0.
我只是好奇:
在这种情况下,为什么C++会以不同方式处理内置类型和自定义类型?
为什么a是true和b是false?或者换句话说,为什么T在foo1中int const,但返回的类型foo2就是int?
template<typename T>
constexpr bool foo1(T &) {
    return std::is_const<T>::value;
}
template<typename T>
T foo2(T &);
int main() {
    int const x = 0;
    constexpr bool a = foo1(x);
    constexpr bool b = std::is_const<decltype(foo2(x))>::value;
}
c++ ×3
c++11 ×1
const ×1
overloading ×1
standards ×1
template-argument-deduction ×1
templates ×1