- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
考虑以下定义。
char right_string[]="::right_one.";
char wrong_string[]="::wrong_one.";
template<const char* str>
void f(){
static_assert(str==::right_string, "Pass me ::right_string!");
}
struct Test{
static constexpr char right_string[]="template_struct::right_one";
static constexpr char wrong_string[]="template_struct::wrong_one";
template<const char* str>
static void f(){
static_assert(str==right_string, "Pass me template_struct::right_string!");
}
};
int main(){
f< ::right_string>(); //compiles, as expected
f< ::wrong_string>(); //does not compile, as expected
Test::f<Test::right_string>(); //compiles, as expected
Test::f<Test::wrong_string>(); //error in Test::f: non-constant condition for static assertion
}
完整的错误是
../main.cpp:16:3: error: non-constant condition for static assertion
../main.cpp:16:3: error: ‘(((const char*)(& Test::wrong_string)) == ((const char*)(& Test::right_string)))’ is not a constant expression
我认为这是一个编译器错误,因为 static_assert
中表达式的 constexpr
ness 根据我作为模板参数(Test::right_string
或 Test::right_string
)。
我已经发现 g++ 4.6 是 somewhat flawed将地址作为模板参数处理时。这是同一个错误的实例吗?
最佳答案
这是一个 g++ 错误,(至少)已在 4.7 中修复。
关于c++ - static_assert 无法将 const char* 模板参数识别为 constexpr : g++ bug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10405835/
我正在尝试创建一个由 std::initializer_list 初始化的示例数组类。我想在编译时检查初始化列表的大小是否不超过数组的大小。由于静态断言只能评估 constexpr,因此不会编译此代码
这是另一个 question 的后续内容 我试图在编译时确定特定实现是否在结构中添加了未命名的填充。像 gcc 这样的特定实现允许使用编译指示来控制结构中的填充和对齐,但代价是与其他实现兼容。由于 C
因此,我正在寻找一种方法,如果用于声明对象的值等于另一个值(不希望使用 C 的 assert 宏),则会导致编译时错误. 是的,我知道为什么会出现这个问题......编译器在他/她提示expressi
是否可以对数组值进行编译时检查? 例子: typedef enum { dummy0 = 0, dummy1, dummy2 } eDummyEnum; typedef str
我的问题是,下面的代码是否有效: template class Class { static_assert(sizeof(i) == 0, "Class instantiated with i
我正在尝试检查参数包中的每个参数是否可以存储在 8 个字节以内(sizeof inline auto invoke(std::uint64_t hash, Arguments... argument
我试图禁止对我已有的类的引用,但我看到了一些奇怪的行为。我已经构建了一个玩具示例来展示正在发生的事情。如果我有这个: template struct something { }; template
我有这样的情况: #define FOO(Readonly) static_assert(Readonly, "Fire!"); Readonly 显然会按字面意思粘贴为“false”或“true”,
我有一个函数模板,它接受一个输出迭代器参数。我如何使用 static_assert 检查实例化是否使用了适当的迭代器? (即,它既是一个输出迭代器,又分配正确类型的元素。) #include #in
在 C++11 中是否有一种优雅的方式来执行条件 static_assert 例如: template class MyClass { COMPILE_TIME_IF( IsTypeBuil
有(不要担心这段代码的长度,专注于struct X和Range)(你可以复制和粘贴并且应该编译): 已编辑 #include #include //This is from file "S
我在编译时过滤一个元组,如果元组为空或者元组包含多个元素,我想输出一条自定义错误消息。 static_assert 似乎有点太复杂了,因为我需要使用 bool 逻辑。 static_assert(si
现代优化 C++ 编译器是否受益于 static_asserts? 例如,如果我断言一个整数只能在一个受限范围内,编译器是通过优化步骤携带该信息还是它仍然只是一个整数? 请不要回答编译器可以考虑它。问
我想做一些static dispatch的工作,让基类static_cast this指针指向派生类并调用同名函数来实现多态。我还想用 static_assert 来确保派生类确实重载了特定功能(否则
我有一个模板函数来操作递归数据结构。在调用的初始函数中,我有以下形式: template auto get(param i) -> int { static_assert(Pos (i.re
我有一个函数模板定义如下: template Test &operator Test &operator::value || std::is_same::valu
我有这段代码来确定模板类型是否有 foo(): template struct has_foo : std::false_type {}; template struct has_foo().fo
我想使用 static_assert 对我的类的配置实现各种限制。早些时候,我只使用一个枚举,并且只允许一个需要所述枚举的构造函数来对我的类实现限制。如果我有类似下面的内容并且范围是从 0 到 4,这
为什么 std 库不使用这些呢?目前,如果对不可复制对象的复制构造函数进行调用,则错误消息可能有点“神秘”或让以前从未遇到过的人感到困惑。 我第一次收到此错误消息时,我不知道问题出在哪里(我从未想过不
为什么主体中的这个 static_assert 比我正在检查的错误早于代码中发生的错误?这是标准行为还是特定于编译器? 我很惊讶地发现这是我想要实现的: template Fizzer_t::Fizz
我是一名优秀的程序员,十分优秀!