有什么方法可以强制编译失败? [英] Any way to force compile failure?
问题描述
如果特定的编译时间
常量不能评估为真,有没有办法强制编译失败?
考虑一个模板,它只适用于未签名的
整数(无论是char,short,int,long):
#include< limits> ;
模板< class T>
void Func()
{
CompileErrorIfTrue(std :: numeric_limits< T> :: is_signed);
/ *其余功能* /
}
我想我们可以做类似的事情:
模板< bool条件>
void CompileErrorIfTrue();
/ *缺少定义以触发编译错误* /
模板<>
void CompileErrorIfTrue< false>(){}
#include<限制>
模板< class T>
void Func()
{
CompileErrorIfTrue<的std :: numeric_limits< T> :: is_sig n ed>();
/ *其余功能* /
}
int main()
{
Func< int>();
}
-
>
Frederick Gotham
Is there any way to force compile failure if a particular compile-time
constant doesn''t evaluate to true?
Consider a template which was intended to only work with unsigned
integers (whether they be char, short, int, long):
#include <limits>
template<class T>
void Func()
{
CompileErrorIfTrue( std::numeric_limits<T>::is_signed );
/* Rest of function */
}
I suppose we could do something like:
template<bool condition>
void CompileErrorIfTrue();
/* Definition missing in order to trigger compile error */
template<>
void CompileErrorIfTrue<false>() {}
#include <limits>
template<class T>
void Func()
{
CompileErrorIfTrue<std::numeric_limits<T>::is_sign ed>();
/* Rest of function */
}
int main()
{
Func<int>();
}
--
Frederick Gotham
推荐答案
Frederick Gotham写道:
Frederick Gotham wrote:
有没有办法强制编译失败如果一个特定的编译时间
常量是否评估为真?
考虑一个模板,该模板仅用于无符号整数(无论是char,short,int) ,长):
#include< limits>
模板< class T>
void Func()
{
CompileErrorIfTrue( std :: numeric_limits< T> :: is_signed);
/ *其余功能* /
}
我想我们可以这样做:
模板< bool条件>
void CompileErrorIfTrue();
/ *缺少定义以触发编译错误* /
模板<& gt;
void CompileErrorIfTrue< false>(){}
模板< class T>
void Func()
{CompileErrorIfTrue< std :: numeric_limits< T> :: is_sign ed>();
/ *其余功能* /
}
int main()
{
Func< int>();
}
-
Frederick Gotham
Is there any way to force compile failure if a particular compile-time
constant doesn''t evaluate to true?
Consider a template which was intended to only work with unsigned
integers (whether they be char, short, int, long):
#include <limits>
template<class T>
void Func()
{
CompileErrorIfTrue( std::numeric_limits<T>::is_signed );
/* Rest of function */
}
I suppose we could do something like:
template<bool condition>
void CompileErrorIfTrue();
/* Definition missing in order to trigger compile error */
template<>
void CompileErrorIfTrue<false>() {}
#include <limits>
template<class T>
void Func()
{
CompileErrorIfTrue<std::numeric_limits<T>::is_sign ed>();
/* Rest of function */
}
int main()
{
Func<int>();
}
--
Frederick Gotham
是的。 Boost和Loki各有一个设施。例如:
http:// boost。 org / doc / html / boost_staticassert.html
干杯! --M
Yes. Boost and Loki each have a facility for this. See for instance:
http://boost.org/doc/html/boost_staticassert.html
Cheers! --M
2006年6月27日星期二14:33:34 GMT,Frederick Gotham
< fg ** *****@SPAM.com>写道:
On Tue, 27 Jun 2006 14:33:34 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote:
如果某个特定的编译时间
常量不能评估为真,有没有办法强制编译失败?
考虑一个仅用于工作的模板无符号整数(无论是char,short,int,long):
#include< limits>
模板< class T>
void Func()
{
CompileErrorIfTrue(std :: numeric_limits< T> :: is_signed);
/ *其余功能* /
}
Is there any way to force compile failure if a particular compile-time
constant doesn''t evaluate to true?
Consider a template which was intended to only work with unsigned
integers (whether they be char, short, int, long):
#include <limits>
template<class T>
void Func()
{
CompileErrorIfTrue( std::numeric_limits<T>::is_signed );
/* Rest of function */
}
尝试:
#define COMPILE_TIME_ASSERT(expr)\
typedef char compile_time_constraint [(expr )? 1:-1]
祝福,
Roland Pibinger
Try:
#define COMPILE_TIME_ASSERT(expr) \
typedef char compile_time_constraint[(expr)? 1:-1]
Best wishes,
Roland Pibinger
Roland Pibinger写道:
Roland Pibinger wrote:
#define COMPILE_TIME_ASSERT(expr)\
typedef char compile_time_constraint [(expr)? 1:-1]
#define COMPILE_TIME_ASSERT(expr) \
typedef char compile_time_constraint[(expr)? 1:-1]
模板可以更清洁,更自我记录吗?考虑由此产生的错误
消息...
-
Phlip
Could a template be cleaner and more self-documenting? Consider the error
message thus produced...
--
Phlip
这篇关于有什么方法可以强制编译失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!