考虑以下情况
typedef void (*foo)();
template<foo f>
struct bar {
static_assert(f!=nullptr,"f == null!");
};
void baz() {}
inline void bax() { }
bar<baz> ok;
bar<bax> bad; // error: non-constant condition for static assertion
Run Code Online (Sandbox Code Playgroud)
这两个baz和bax被接受为模板参数.它表明两者都被接受为常量.然而,static_assert它们似乎是不同的(至少在gcc 4.9中) - bax不再是常数.
我的假设是static_assert和模板相同地评估常数.例如,错误应该是
static_assert 不应该引起非恒定条件错误.我错了吗?
如图所示,如果将具有多个参数的模板实例化作为参数传递给宏,则C++预处理器将失败.
请参阅下面的示例.
#include <stdio.h>
#define FOO(v) printf("%d\n",v::val())
template<int N>
struct bar {
static int val() { return N; }
};
template<int N, int M>
struct baz {
static int val() { return N+M; }
};
int main() {
printf("%d\n",bar<1>::val());
printf("%d\n",baz<1,2>::val());
FOO(bar<10>); // OK
FOO(baz<20,30>); // error: too many arguments provided to function-like macro invocation
FOO((baz<20,30>)); // error: '::val' has not been declared
}
Run Code Online (Sandbox Code Playgroud)
用clang ++和g ++ 测试
它应该被视为一个错误吗?
刚刚下载的NDK在链接旧项目时失败:
/usr/bin/ld: unrecognised emulation mode: armelf_linux_eabi
看来,clang隐藏在gcc的面具后面:
/opt/android-ndk-r18b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc --version
Android (4751641 based on r328903) clang version 7.0.2 (https://android.googlesource.com/toolchain/clang 003100370607242ddd5815e4a043907ea9004281) (https://android.googlesource.com/toolchain/llvm 1d739ffb0366421d383e04ff80ec2ee591315116) (based on LLVM 7.0.2svn)
Target: arm--linux-android
Thread model: posix
InstalledDir: /opt/android-ndk-r18b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../../../../llvm/prebuilt/linux-x86_64/bin
Run Code Online (Sandbox Code Playgroud)
它只是这个NDK版本中的一个错误还是故意制作的?
是否有一个模板或方法哪一个可以用声明来区分signed int,并int在下面的例子中,像
signed int foo;
int bar;
static_assert(magic_signed<decltype(foo)>::value,"signed"); //PASS
static_assert(magic_signed<decltype(bar)>::value,"not signed"); //ASSERT
Run Code Online (Sandbox Code Playgroud) c++ ×4
templates ×3
android ×1
android-ndk ×1
arguments ×1
function ×1
inline ×1
parameters ×1
signed ×1
type-traits ×1