使用以下宏:
#define ASSERT_IF_TEMP(expr) static_assert(?, "Is temporary!");
Run Code Online (Sandbox Code Playgroud)
我该怎么做问号?
代码示例:
template <int x>
struct SUM
{
static_assert(x >= 0, "X must be greater or equal to 0");
enum {VALUE = x + SUM<x-1>::VALUE};
};
template<>
struct SUM<0>
{
enum {VALUE = 0};
};
int main()
{
std::cout << SUM<-1>::VALUE << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器不会破坏第一个static_assert上的编译,但继续工作直到达到最大实例化深度?
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/Main.d" -MT"src/Main.d" -o "src/Main.o" "../src/Main.cpp"
../src/Main.cpp: In instantiation of ‘struct SUM<-1>’:
../src/Main.cpp:47:22: required from here
../src/Main.cpp:26:2: error: static assertion failed: X …Run Code Online (Sandbox Code Playgroud) 当表达式取决于类类型本身时,有没有办法在类内进行 static_assert ?也许延迟评估直到类型完成或模板实例化之后?
示例代码:
#include <type_traits>
template<typename T>
struct Test {
T x = 0; // make non-trivial
static_assert(std::is_trivial<Test<T>>::value, "");
};
int main() {
// would like static assert failure, instead get 'incomplete type' error
Test<int> test1;
Test<float> test2;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 此代码在 MSVC 上编译失败,因为static_assert失败:
template<class MyType>
struct Test {
static_assert(MyType(5) != MyType(6), "fails");
};
Run Code Online (Sandbox Code Playgroud)
请参阅: https: //godbolt.org/z/vUSMHu
任何想法 MSVC 如何在不知道 MyType 是什么的情况下评估它?
更晦涩的是:
template<class MyType>
struct Test {
static_assert(MyType(5) == MyType(6), "succeeds");
static_assert(!(MyType(5) == MyType(6)), "fails");
};
Run Code Online (Sandbox Code Playgroud)
参见: https: //godbolt.org/z/3631tu
实例化它(即给 MyType 一个类型)也没有帮助:
template<class MyType>
struct Test {
static_assert(MyType(5) != MyType(6), "still fails");
};
Test<int> variable;
Run Code Online (Sandbox Code Playgroud)
请参阅: https: //godbolt.org/z/yxF4h0
或者更复杂一点: https: //godbolt.org/z/68g6yO
c++ static-assert visual-studio visual-c++ visual-studio-2017
我有4个int常量:
const int a1 = 1024;
const int a2 = 768;
const int b1 = 640;
const int b2 = 480;
Run Code Online (Sandbox Code Playgroud)
我想静态检查它们是否具有相同的比例.要静态检查,我正在使用BOOST_STATIC_ASSERT,但它不支持表达式.
我试过这个:
BOOST_STATIC_ASSERT( 1e-5 > std::abs( (double)a1 / (double)a2 - (double)b1 / (double)b2 ) );
Run Code Online (Sandbox Code Playgroud)
但这会产生下一个编译错误:
error: floating-point literal cannot appear in a constant-expression
error: 'std::abs' cannot appear in a constant-expression
error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
error: a cast to a type other than …Run Code Online (Sandbox Code Playgroud) 我有几个带有"static const"数据成员的类.我想知道如何使用static_assert在编译时检查它们的值.我可以将static_assert直接放在类体中吗?(将static_assert放在每个构造函数中都不太实用.)
根据此答案, C#现在具有“代码约定”,该代码约定应该可用,而不是C ++编译时断言。现在我有了这个魔术代码:
IntPtr pointer;
//blahblahblah
pointer = new IntPtr(pointer.ToInt32() + Marshal.SizeOf(typeof(SomeStruct)));
Run Code Online (Sandbox Code Playgroud)
需要IntPtr是相同大小的Int32。所以我想要一个编译时间断言-像这样的C ++代码
static_assert(sizeof(IntPtr)==sizeof(Int32))
Run Code Online (Sandbox Code Playgroud)
所以我尝试了以下方法:
System.Diagnostics.Contracts.Contract.Assert(false); //just to test it
pointer = new IntPtr(pointer.ToInt32() + Marshal.SizeOf(typeof(SomeStruct)));
Run Code Online (Sandbox Code Playgroud)
我false进入Assert()以便它肯定会失败,但是编译通过就可以了。
那么,如何使用代码协定来声明编译时间呢?
我有一些通用代码需要对成员函数的结果运行断言.该成员函数可以是constexpr,也可以不是.
template<typename T>
void foo(T t) {
assert(t.member_function() == 10);
}
Run Code Online (Sandbox Code Playgroud)
因为t.member_function() 可能是一个常量表达式,我想知道static_assert在这种情况下是否可以将其视为a ,但是否则默认为正常assert.这可能吗?
我有一个模板化的功能,我想static_assert它的类型有三个大小.此代码说明了我正在尝试做什么,但不起作用:
template < typename T >
void foo( T& param )
{
// This line is the one that I need to figure out how to write
static_assert( 3 == std::extent< T >::value, "param must have a size of 3" );
}
int main( void )
{
int cArray[3];
std::array< int, 3 > stdArray;
foo( cArray );
foo( stdArray );
}
Run Code Online (Sandbox Code Playgroud) 在下面的示例main中,static_assert如果字符串文字以开头'v',则verify可以,但是不能。
为什么会这样呢?有没有一种方法可以verify对static_assert字符串文字中的字符进行条件设置?
#include <cstddef>
template <std::size_t N>
constexpr char get_first(const char (&str)[N])
{
static_assert(N>1, "must be > 1");
return str[0];
}
template <std::size_t N>
constexpr void verify(const char (&str)[N])
{
static_assert(str[0] == 'v', "must start from v");
}
int main()
{
static_assert(get_first("value") == 'v', "first must be 'v'"); // succeeds
verify("value"); // fails to compile
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
main.cpp: In instantiation of 'constexpr void verify(const char (&)[N]) [with long …Run Code Online (Sandbox Code Playgroud) static-assert ×10
c++ ×9
c++11 ×3
templates ×3
arrays ×1
assert ×1
boost ×1
c# ×1
c++14 ×1
class ×1
constexpr ×1
type-traits ×1
visual-c++ ×1