C++ 在宏中使用静态断言
我的情况是这样的:C++ 在宏中使用静态断言,c++,c++11,C++,C++11,我的情况是这样的: #define FOO(Readonly) static_assert(Readonly, "Fire!"); 只读显然会按字面意思粘贴为“false”或“true”,因此静态断言将始终触发。如何编写一个条件来代替只读,以便静态断言正常工作 以下是我的预期用途: #define CAT(x, y) CAT_(x, y) #define CAT_(x, y) x ## y #define GET_SET(Name, Readonly) decltype(Name) CAT(
#define FOO(Readonly) static_assert(Readonly, "Fire!");
只读
显然会按字面意思粘贴为“false”或“true”,因此静态断言
将始终触发。如何编写一个条件来代替只读
,以便静态断言
正常工作
以下是我的预期用途:
#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!");
请记住,当条件为false时,static\u 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; }
看来您刚刚忘记了代码>宏中只读的前面
static\u 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);
};
定义类别(x,y)类别(x,y)
#定义猫(x,y)x##y
#定义GET_SET(Name,Readonly)decltype(Name)CAT(GET,Name)()const{return Name;}\
void CAT(set,Name)(decltype(Name)value=decltype(Name)(){\
静态断言(!Readonly,#Name“是只读的。”)\
名称=值\
}
样板
福班
{
int x;
int-y;
公众:
Foo():x(0),y(0){}
获取_集(x,false);
获取设置(y,true);
};
当然,除此之外,它在坏情况下会呕吐,因为我们使用了static\u assert
而不是运行时的assert或throw。但它会执行您声明的操作。宏指令
#define FOO(Readonly) static_assert(Readonly, "Fire!");
正如您正确推测的那样,会将传递的值转发到Readonly,因此
FOO(false)
将产生
static_assert(false, "Fire!");
请记住,当条件为false时,static\u 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; }
看来您刚刚忘记了代码>宏中只读的前面
static\u 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);
};
定义类别(x,y)类别(x,y)
#定义猫(x,y)x##y
#定义GET_SET(Name,Readonly)decltype(Name)CAT(GET,Name)()const{return Name;}\
void CAT(set,Name)(decltype(Name)value=decltype(Name)(){\
静态断言(!Readonly,#Name“是只读的。”)\
名称=值\
}
样板
福班
{
int x;
int-y;
公众:
Foo():x(0),y(0){}
获取_集(x,false);
获取设置(y,true);
};
当然,除此之外,它在坏情况下会呕吐,因为我们使用了
static\u assert
而不是运行时的assert或throw。但是它做了你想让它做的事。什么是Readonly
?为什么它会使断言总是激发?这有什么不起作用?@aaronman这可能是一个X和Y问题,但它似乎总是激发我所说的原因。我测试了它,它似乎有效fine@aaronman我贴了一个Coliru链接。我的意图是,只有当您试图调用sety
时,才会触发static_assert。什么是Readonly
?为什么它会使断言总是激发?这有什么不起作用?@aaronman这可能是一个X和Y问题,但它似乎总是激发我所说的原因。我测试了它,它似乎有效fine@aaronman我贴了一个Coliru链接。我的意图是,只有当您试图调用sety
时,才会触发static_assert。