如果可能的话,在编译时检查一个值
Check a value at compile time if possible
如果一个值是constexpr
值,我想在编译时检查它,如果是,我想对它运行constexpr
表单检查函数。
我可以在c++11/14中执行此操作吗?
在伪代码中,我想做:
static_if (is_constexpr(x))
static_assert(check(x))
详细信息:
我正在检查字符串中的特定格式。从概念上讲:
template<std::size_t N>
constexpr bool check(const char (&s)[N]){ return true; }
//^ really a recursive call that checks the string
constexpr检查适用于字符串文字,但不适用于字符串指针:
int main(){
const char* s = "#";
static_assert(check("#"), "Fmt");; //OK
//This fails; expectation was it suceeds and the check doesn't run
static_assert(!is_constexpr(s) || check(s), "Fmt");
}
is_constexpr
是:
#include <type_traits>
template<typename T>
constexpr typename std::remove_reference<T>::type makeprval(T && t) {
return t;
}
#define is_constexpr(e) noexcept(makeprval(e))
也许你的问题对我来说有点不清楚。如果你想知道字符串文字是否应该通过,指针是否应该不通过,那么看看下面的答案。如果你觉得,我误解了你的问题,然后发表评论,我会删除这个答案或相应地修改。
您可以使用简单的C样式宏来检查给定的变量是否是字符串文字,正如我在回答以下问题时所解释的:
验证传递给函数的字符串类型(例如文字、数组、指针)
#define IS_LITERAL(X) "" X
如果X
不是字符串文字,则编译将失败。
相关文章:
- 如何检查一个c++字符串中有多少相同的字符/数字
- 检查一个类型是否直接派生自"enable if"上下文中的另一个类型(是其子类型)
- 我们应该在使用 std::bind 应用之前检查一个不为空的函数吗?
- 如何检查一个模板是否是另一个模板的类成员
- 如何检查一个字符是否与字符数组中的另一个字符匹配?
- 如何检查一个字符串是否包含多个其他字符串?
- 如何以更有效的方式检查一个数字是否是素数?
- C++显示两个区间之间的数字的程序检查一个数字是否可以表示为两个素数的总和
- 检查一个文本文件中的单词是否在另一个文本文件 (C++) 中
- 我无法完全理解这段代码来检查一个完美的正方形
- 如何检查一个库是否可以由特定的g++版本编译?
- (UE4)检查一个点是否位于碰撞圆圈中
- 更好的算法来检查一个数字是否既不是素数也不是单个素数的幂
- 如何检查一个名称是否与另一个名称引用相同的符号
- 检查一个数字是否均匀地划分为其他许多人
- 在构造函数中初始化const字段,但首先检查一个参数
- 检查一个字符串是否是另一个字符串的排列C++
- 检查一个特定的过程是否在Windows上使用C 运行
- 如何检查一个范围内的值是否是另一个范围内的值的倍数
- 为什么使用两个sizeofs可以检查一个类是否默认可构造,而使用一个却不行