c++ - constexpr if 和 static_assert

标签 c++ templates constexpr c++17 static-assert

P0292R1 constexpr if一直included ,在 C++17 的轨道上。它似乎很有用(并且可以替代 SFINAE 的使用),但是关于 static_assert 的评论是 错误的,不需要诊断 在 false 分支中吓到我了:

Disarming static_assert declarations in the non-taken branch of a
constexpr if is not proposed.

void f() {
  if constexpr (false)
    static_assert(false);   // ill-formed
}

template<class T>
void g() {
  if constexpr (false)
    static_assert(false);   // ill-formed; no 
               // diagnostic required for template definition
}

我认为完全禁止在 constexpr if 中使用 static_assert(至少是 false/non-taken 分支,但在实践中这意味着这不是一件安全或有用的事情)。

这是如何从标准文本中得出的?我发现提案措辞中没有提到 static_assert,C++14 constexpr 函数确实允许 static_assert(cppreference 中的详细信息:constexpr)。

它是否隐藏在这个新句子中(6.4.1 之后)? :

When a constexpr if statement appears in a templated entity, during an instantiation of the enclosing template or generic lambda, a discarded statement is not instantiated.

从那以后,我假设也禁止调用其他 constexpr(模板)函数,这些函数某处在调用图下方可能会调用 static_assert .

底线:

如果我的理解是正确的,那不是对 constexpr if 的安全性和有用性设置了相当严格的限制,因为我们必须知道(通过文档或代码检查)关于任何使用静态断言?我的担心是多余的吗?

更新:

此代码在没有警告的情况下编译 (clang head 3.9.0),但据我了解格式错误,不需要诊断。有效还是无效?

template< typename T>
constexpr void other_library_foo(){
    static_assert(std::is_same<T,int>::value);
}

template<class T>
void g() {
  if constexpr (false)
    other_library_foo<T>(); 
}

int main(){
    g<float>();
    g<int>();
}

最佳答案

这是关于模板的公认规则 - 允许编译器诊断的相同规则 template<class> void f() { return 1; } . [temp.res]/8新变化以粗体显示:

The program is ill-formed, no diagnostic required, if:

  • no valid specialization can be generated for a template or a substatement of a constexpr if statement ([stmt.if]) within a template and the template is not instantiated, or
  • [...]

无法为包含 static_assert 的模板生成有效的特化其条件是独立的,计算结果为 false , 所以程序是病式 NDR。

static_assert具有可以评估为 true 的从属条件至少一种类型不受影响。

关于c++ - constexpr if 和 static_assert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49155428/

相关文章:

c++ - 从(模板)基类内部调用虚拟成员函数

c++ - 为什么我可以在 constexpr 函数中调用非 constexpr 函数?

c++ - Google linked_ptr 的实现和linked_ptr 的用处

c++ - 交换链表中的相邻节点

java - docx 模板 Docx4j 替换 Java 中的文本

c++ - 如何专门化另一个模板类的模板成员函数?

c++ - Enum 不是一个 constexpr?

c++ - 将全局 constexpr 类移动到类中作为静态 constexpr

c++ - 如何在 C/C++ 中捕获现有代码中的行号?

c++ - 调用函数时从 'char' 到 'char*' 的无效转换 (c++)