如果条件为真,我想在非constexpr时引发编译时错误,例如:
if constexpr(condition1){
...
} else if constexpr (condition2) {
....
} else if constexpr (condition3) {
....
} else {
// I want the else clause never taken. But I heard the code below is not allowed
static_assert(false);
}
// I'd rather not repeat the conditions again like this:
static_assert(condition1 || condition2 || condition3);
Run Code Online (Sandbox Code Playgroud) static_assert在 的 false 分支中应丢弃以下内容if constexpr,但由于断言失败而导致编译失败:
#include <type_traits>
template <class T>
constexpr bool f() {
if constexpr (std::is_same<T, int>::value) return true;
else static_assert(false, "message");
}
int main () {
if constexpr (f<int>()) return 1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望丢弃的分支if constexpr不会被评估,因为它f是用 type 实例化的int。
用 Gcc 7.2 (-std=c++17) 编译
std::transform提供采用一元(一个参数)或二元(两个参数)可调用操作(通常为 lambda)的重载。
我想将我想要的可调用对象作为参数传递给父函数,并使用编译时(例如模板元编程)方法std::transform根据传递的可调用对象是否具有带有一个的函数签名来自动选择使用哪个重载或两个论点。
这是用(尚未工作)代码表达的所需方法:
#include <algorithm>
auto UnaryOp = [](const auto& src) { return src; }; // simple copy
auto BinaryOp = [](const auto& src1, const auto& src2) {return src1 + src2; }; // accumulate
auto GenericTransformer = [](auto src, auto dst, auto operation) { // operation is unary OR binary
/* unrelated code */
// need to chose this one:
std::transform(src.begin(), src.end(), dst.begin(), operation);
// or this one:
std::transform(src.begin(), src.end(), dst.begin(), dst.begin(), operation);
// depending on …Run Code Online (Sandbox Code Playgroud) 我昨天读了几个关于在an 子句中使用的答案。我知道根据标准,它被认为是格式错误的(即使某些编译器,包括 MSVC2017,会接受它)。Qt 也会将此标记为错误。static_assert(false, "Some message")elseif constexpr
我的问题是,下面的代码是否符合标准?(我倾向于这么认为,但我想确认一下。)
template <typename TypeOfValue>
static void PushValue(duk_context* ctx, TypeOfValue value) {
// Push value onto duktape stack
if constexpr (std::is_same<TypeOfValue, int>::value) {
// Push int
duk_push_int(ctx, value);
} else if constexpr (std::is_same<TypeOfValue, uint32_t>::value) {
// Push uint
duk_push_uint(ctx, value);
} else {
// Unsupported type
static_assert(bool_value<false, TypeOfValue>(), "Unsupported type");
}
}
template <bool value, typename T>
static constexpr bool bool_value() {return value;}
Run Code Online (Sandbox Code Playgroud)
编辑: …
我花了一些时间学习如何在C++中使用模板.我之前从未使用它们,但我并不总是确定在不同的情况下可能存在什么或者什么是不能实现的.
作为练习,我将包含一些我用于活动的Blas和Lapack函数,我正在进行包装?GELS(评估线性方程组的解).
A x + b = 0
Run Code Online (Sandbox Code Playgroud)
?GELS函数(仅对于实数值)存在两个名称:SGELS,对于单精度向量和
DGELS双精度.
我对接口的想法就是这样一种功能solve:
const std::size_t rows = /* number of rows for A */;
const std::size_t cols = /* number of cols for A */;
std::array< double, rows * cols > A = { /* values */ };
std::array< double, ??? > b = { /* values */ }; // ??? it can be either
// rows or cols. It depends on user
// …Run Code Online (Sandbox Code Playgroud) 我尝试检测一个类是否已在 C++ 中实现。该类要么仅在之前声明过(情况 1),要么已实现(情况 2)。为了检测类的实现,SFINAE 表达式应该评估sizeof(T)模板参数,如果类不完整,则模板参数将失败。
#include <type_traits>
// 1. Feature not supported: Class only declared
class Test;
// 2. Feature supported: Class implemented
// class Test {};
// Detection if class is complete
template<class T, class Enable = void>
struct is_complete
{
static constexpr bool value = false;
};
template<class T>
struct is_complete<T, std::enable_if_t<(sizeof(T) == sizeof(T))>>
{
static constexpr bool value = true;
};
int main(void)
{
if constexpr (is_complete<Test>::value)
{
// static_assert(is_complete<Test>::value, "Test is not complete"); …Run Code Online (Sandbox Code Playgroud) 甲相关的问题提供了一种类型无关的例子false中static_assert:
template<class T> void foo()
{
if constexpr(false)
static_assert(false);
}
Run Code Online (Sandbox Code Playgroud)
但是,我更担心同样的事情是否适用于依赖于类型的false. 这是标准中的相关引用:
如果不能为模板或模板中的 constexpr if 语句的子语句生成有效的特化,并且模板未实例化,则程序格式错误,无需诊断。§ 13.7/8.1
这让我感到惊讶,因为我经常看到以下习语:
template<class T> void foo()
{
if constexpr(cond)
// ...
else
static_assert(!std::is_same_v<T, T>);
}
Run Code Online (Sandbox Code Playgroud)
事实上,cppreference 甚至提供了一个同样的例子:
template<class T> struct dependent_false : std::false_type {};
template<class T> void foo()
{
if constexpr (cond)
// ...
else
static_assert(dependent_false<T>::value);
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,在这两种情况下,都无法为相关if constexpr子语句生成有效的专业化,因此格式不正确,不需要诊断。我对么?
如这个问题所示:link,如果两个if分支都有效,则两者之间没有区别:
const int foo = 5;
if (foo == 5)
{
...
}
else
{
...
}
Run Code Online (Sandbox Code Playgroud)
和
const int foo = 5;
if constexpr (foo == 5)
{
...
}
else
{
...
}
Run Code Online (Sandbox Code Playgroud)
在优化方面(在两种情况下else都不会实例化分支)。因此,如果if可以在编译时检查vanilla中的表达式(它涉及到const或constexpr),那么优化也可以在此处进行。
我以前认为这是的目的if constexpr,但我错了。那么,除了用例之外,还有没有if constexpr其他用例,那么我们可能只有许多if分支有效之一?
我有一个模板函数,使用if constexpr诸如以下命令检查模板参数的类型
template <typename T>
bool something(T arg) {
if constexpr (std::is_integral_v<T>) {
return true;
} else {
// What can I write here so that something<double>(0.0) does not compile?
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
如果没有if constexprs匹配,如何使代码无法编译?
c++ ×9
if-constexpr ×4
constexpr ×3
c++17 ×2
sfinae ×2
assertion ×1
compile-time ×1
dry ×1
gcc ×1
lambda ×1
optimization ×1
overloading ×1
templates ×1