NVCC 和编译器错误的静态断言
在用于编译时设置的结构中为 NVCC 编译器提供静态断言的最佳方法是什么:
以下内容大部分有效,但有时 NVCC 会产生胡言乱语的错误消息,并且即使应该编译也不会编译!
template<int A, int B>
struct Settings{
static const int a = A;
static const int b = B;
STATIC_ASSERT(a == 15);
}
typedef Settings<15,5> set1; // Comment this out and it works....
template<int A, int B>
struct Settings2{
static const int a = A;
static const int b = B;
STATIC_ASSERT(a % b == 0);
}
typedef Settings<10,5> set2;
静态断言不起作用,我不知道,但有一个 CUDA 编译器 BUG 告诉我,当我编译时,它会抛出 STATIC_ASSERT(a == 15);即使它应该编译,因为上面的代码是正确的,如果我注释掉(A)那么它会突然起作用, 我使用 Thrust 中的 STATIC_ASSERT,它基本上取自 Boost:
#define JOIN( X, Y ) DO_JOIN( X, Y )
#define DO_JOIN( X, Y ) DO_JOIN2(X,Y)
#define DO_JOIN2( X, Y ) X##Y
namespace staticassert {
// HP aCC cannot deal with missing names for template value parameters
template <bool x> struct STATIC_ASSERTION_FAILURE;
template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
// HP aCC cannot deal with missing names for template value parameters
template<int x> struct static_assert_test{};
};
// XXX nvcc 2.3 can't handle STATIC_ASSERT
#if defined(__CUDACC__) && (CUDA_VERSION > 100)
#error your version number of cuda is not 2 digits!
#endif
#if defined(__CUDACC__) /* && (CUDA_VERSION < 30)*/
#define STATIC_ASSERT( B ) typedef staticassert::static_assert_test<sizeof(staticassert::STATIC_ASSERTION_FAILURE< (bool)( (B) ) >) > JOIN(thrust_static_assert_typedef_, __LINE__)
#define STATIC_ASSERT2(B,COMMENT) STATIC_ASSERT(B)
#else
#define STATIC_ASSERT2(B,COMMENT) \
typedef staticassert::static_assert_test< \
sizeof(staticassert::STATIC_ASSERTION_FAILURE< (bool)( (B) ) >)>\
JOIN(thrust_static_assert_typedef_, JOIN(__LINE__, COMMENT ))
#define STATIC_ASSERT( B ) \
typedef staticassert::static_assert_test<sizeof(staticassert::STATIC_ASSERTION_FAILURE< (bool)( (B) ) >) > JOIN(thrust_static_assert_typedef_, __LINE__)
#endif // NVCC 2.3
有人遇到过同样的问题吗?
感谢您的任何评论!
Whats the best way to have a static assert for the NVCC compiler inside a struct which is used for compile time settings:
The following works mostly but sometimes NVCC produces bullshit error messages, and does not compile even if it should!
template<int A, int B>
struct Settings{
static const int a = A;
static const int b = B;
STATIC_ASSERT(a == 15);
}
typedef Settings<15,5> set1; // Comment this out and it works....
template<int A, int B>
struct Settings2{
static const int a = A;
static const int b = B;
STATIC_ASSERT(a % b == 0);
}
typedef Settings<10,5> set2;
The static assert does not work, I dont know but there is a CUDA Compiler BUG which tells me when I compile it throws the STATIC_ASSERT(a == 15); even if IT should COMPILE because the code above is correct, if I comment (A) out then it suddenly works,
I use the STATIC_ASSERT from Thrust which is basically taken from Boost:
#define JOIN( X, Y ) DO_JOIN( X, Y )
#define DO_JOIN( X, Y ) DO_JOIN2(X,Y)
#define DO_JOIN2( X, Y ) X##Y
namespace staticassert {
// HP aCC cannot deal with missing names for template value parameters
template <bool x> struct STATIC_ASSERTION_FAILURE;
template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
// HP aCC cannot deal with missing names for template value parameters
template<int x> struct static_assert_test{};
};
// XXX nvcc 2.3 can't handle STATIC_ASSERT
#if defined(__CUDACC__) && (CUDA_VERSION > 100)
#error your version number of cuda is not 2 digits!
#endif
#if defined(__CUDACC__) /* && (CUDA_VERSION < 30)*/
#define STATIC_ASSERT( B ) typedef staticassert::static_assert_test<sizeof(staticassert::STATIC_ASSERTION_FAILURE< (bool)( (B) ) >) > JOIN(thrust_static_assert_typedef_, __LINE__)
#define STATIC_ASSERT2(B,COMMENT) STATIC_ASSERT(B)
#else
#define STATIC_ASSERT2(B,COMMENT) \
typedef staticassert::static_assert_test< \
sizeof(staticassert::STATIC_ASSERTION_FAILURE< (bool)( (B) ) >)>\
JOIN(thrust_static_assert_typedef_, JOIN(__LINE__, COMMENT ))
#define STATIC_ASSERT( B ) \
typedef staticassert::static_assert_test<sizeof(staticassert::STATIC_ASSERTION_FAILURE< (bool)( (B) ) >) > JOIN(thrust_static_assert_typedef_, __LINE__)
#endif // NVCC 2.3
Did anybody experience the same problem?
Thanks for any comments!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在每个 struct 定义后添加缺少的分号后,您的代码编译时不会出现任何警告或错误。系统详细信息:
After adding the missing semicolons after each
struct
definition, your code compiles with no warnings or errors for me. System details: