相关疑难解决方法(0)

什么是顶级const限定符?

是什么const在"顶级"预选赛中,C平均++?

还有什么其他水平?

例如:

int const *i;
int *const i;
int const *const i;
Run Code Online (Sandbox Code Playgroud)

c++ const

46
推荐指数
4
解决办法
9028
查看次数

当从函数返回为"const"时,为什么原始类型和用户定义类型的行为不同?

#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.
}
Run Code Online (Sandbox Code Playgroud)

问题描述嵌入在代码中.我的编译器是clang 5.0.

我只是好奇:

在这种情况下,为什么C++会以不同方式处理内置类型和自定义类型?

c++ standards overloading overload-resolution c++11

41
推荐指数
3
解决办法
2065
查看次数

模板函数类型推导和返回类型

为什么atruebfalse?或者换句话说,为什么Tfoo1int 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;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates template-argument-deduction

4
推荐指数
1
解决办法
206
查看次数