当前的C++标准使编译器能够查看函数体以获取完成函数签名的信息:
template <typename T>
inline auto dereference(T const& pointer) {
return *pointer;
}
Run Code Online (Sandbox Code Playgroud)
我想知道如果可以看到定义,编译器是否可以自动声明函数是否保证,可能使用如下语法:
void func() noexcept(auto) {
… // If all operations in the function are noexcept, the
// function can be automatically declared noexcept
}
Run Code Online (Sandbox Code Playgroud)
如果可能,那么在编写noexcept声明时会省去很多麻烦,特别是在编写模板时,函数通常会在第一次出现时定义:
// The noexcept declaration can be even longer than the function body
template <typename T>
void func(T& value) noexcept(
noexcept(value.member_1()) &&
noexcept(value.member_2()) &&
noexcept(value.member_3())
) {
value.member_1();
value.member_2();
value.member_3();
}
// Things will become much easier if the C++ standard support this …Run Code Online (Sandbox Code Playgroud) 据我所知,ISO C 标准对 C11 6.7.9 中具有静态存储持续时间的对象的初始值设定项很严格
具有静态或线程存储持续时间的对象的初始值设定项中的所有表达式应为常量表达式或字符串文字。
但是 GCC/Clang 都接受了以下代码:
const int i = 3; // const here should not make i a constant expression
static int j = i;
Run Code Online (Sandbox Code Playgroud)
即使有-Wall -Wextra -Werror -pedantic-errors,上面的编译器也没有给我任何抱怨。
但是,这些编译器确实意识到这i不是一个常量表达式。例如,Clang 给了我:
错误:静态数组的大小必须是整数常量表达式
对于以下代码:
const size_t sz = 3;
static int a[sz];
Run Code Online (Sandbox Code Playgroud)
我在这里有什么问题吗?