结构内部允许静态断言吗?
我有几个模板设置结构,可以在这些结构中使用静态断言吗?
template<typename T, int N, (and so on...)>
struct Settings{
static const int n = N;
STATIC_ASSERT(n == 5);
typedef typename T GAGA;
}
感谢您的回复!
I have several template settings struct, is it ok, to use static asserts in these structs?
template<typename T, int N, (and so on...)>
struct Settings{
static const int n = N;
STATIC_ASSERT(n == 5);
typedef typename T GAGA;
}
Thanks for your responses!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道你的 STATIC_ASSERT 是什么,但如果你使用 c++11 风格的 static_assert 编写它,那么它工作得很好,并且看起来非常适合静态断言。 (好吧,也许不检查它是 5,而是检查模板参数是否适合实例化)
I don't know what your STATIC_ASSERT is but if you write it using c++11 style static_assert then this works fine and seems like a perfectly good use for static assert. (Well, perhaps not checking it's 5 but checking template parameters are suitable for instantating)
您必须查看 STATIC_ASSERT 宏定义才能了解到底发生了什么。可以在此处使用的 STATIC_ASSERT 的常见实现可能是:
通常需要一些技巧才能使行号成为 typedef 的一部分,以便多个 STATIC_ASSERT 可以使用> 可以在相同的上下文中使用,但您可以看到这是允许的,因为它将扩展到结构定义中的有效代码:
You would have to look at the
STATIC_ASSERT
macro definition to see what is exactly going on. A common implementation ofSTATIC_ASSERT
that can be used there could be:Usually there is a bit more trickery to get the line number to be part of the typedef so that more than one
STATIC_ASSERT
can be used in the same context, but you can see that this is allowed as it will expand to valid code in the struct definition:我没有看到使用
n
的正当理由。I dont see a valid reason to use
n
.