在宏中使用static_assert
Using a static_assert in a macro
我有这样的情况:
#define FOO(Readonly) static_assert(Readonly, "Fire!");
Readonly
显然会从字面上粘贴为" false"或" true",因此static_assert
将始终发射。如何编写条件代替Readonly
,以使static_assert
正常工作?
这是我的预期用法:
#define CAT(x, y) CAT_(x, y)
#define CAT_(x, y) x ## y
#define GET_SET(Name, Readonly) decltype(Name) CAT(get, Name)() const { return Name; }
void CAT(set, Name)(decltype(Name) value = decltype(Name)()) {
static_assert( /* Insert Magic bullet here */ , #Name " is read-only.");
Name = value;
}
class Test
{
int x;
int y;
public:
GET_SET(x, false)
GET_SET(y, true)
};
示例预处理器输出:
decltype(x) getx() const { return x; } void setx(decltype(x) value = decltype(x)()) { static_assert(!false, "x" " is read-only."); x = value; }
decltype(y) gety() const { return y; } void sety(decltype(y) value = decltype(y)()) { static_assert(!true, "y" " is read-only."); y = value; }
实时示例
宏指令
#define FOO(Readonly) static_assert(Readonly, "Fire!");
正如您正确推测的那样,将转发传递到ReadOnly的值,因此
FOO(false)
将生成
static_assert(false, "Fire!");
请记住,static_assert
断言条件是错误的,这将始终发射。但是
FOO(true);
// generates
static_assert(true, "Fire!");
永远不会断言。
在您所需的输出中,您写了:
decltype(x) getx() const { return x; } void setx(decltype(x) value = decltype(x)()) { static_assert(!false, "x" " is read-only."); x = value; }
看来您只是在宏中忘记了readonly的!
。
static_assert
是一个编译时间关键字,在编译时间检查,而不是运行时间,因此除非有某种原因在实例化之前无法解决该问题(例如,它是针对模板类型的成员变量或模板进行检查的参数)然后它总是会在声明时失败。
以下代码似乎正常:
#define CAT(x, y) CAT_(x, y)
#define CAT_(x, y) x ## y
#define GET_SET(Name, Readonly) decltype(Name) CAT(get, Name)() const { return Name; }
void CAT(set, Name)(decltype(Name) value = decltype(Name)()) {
static_assert( !Readonly , #Name " is read-only.");
Name = value;
}
template<typename T>
class Foo
{
int x;
int y;
public:
Foo() : x(0), y(0) {}
GET_SET(x, false);
GET_SET(y, true);
};
http://ideone.com/bkrybe
当然,除非它在坏案例上进行了禁止,因为我们已经使用了static_assert
而不是运行时断言或投掷。但这做了您所说的您想要做的事情。
相关文章:
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 收到错误"invalid use of non-static data member 'stu::n' "
- LNK1104:无法打开libpjproject-i386-Win32-vc14-Debug-Static.lib
- 我应该在 C++ 中何时/为什么使用 STATIC?
- 在VS2019项目中集成ImageMagick:x64-windows-static library
- 如何在 assert() 和 static_assert() 之间调度,如果在 constexpr 上下文中依赖?
- 如何处理Boost Spirit X3导致Visual Studio 2019 "static initialization order fiasco"?
- 我关于函数"Assert"的C++代码有问题
- "static char __ = []() -> char"的含义
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 无法在 DLL 中链接 SDL2-static.lib
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- assert() 在发布模式下充当标识函数吗?
- 如何编写一个接受如下断言消息的自定义断言函数:assert(false) << "assertio
- C++线程"Call to non-static member function without an object argument"
- 出现这种错误的原因是什么"invalid use of non-static data member "
- static是如何使用ClassA::m_variable处理所有类对象的
- LNK2001:未解析的外部符号public:static类std::vector
- static assert - C++ concepts vs static_assert