小编Zar*_*doo的帖子

使用 enable_if 单独定义和声明模板成员函数,其模板参数还包括一个 constexpr 成员函数

以下不能在 CentOS 7 上的 g++ 8.1.0 下编译:

嘿.h

#pragma once
#include <iostream>
#include <type_traits>

class Valid {};
class Invalid {};

struct Hey
{
  template<typename T>
  static constexpr bool is_valid() { return std::is_same_v<T, Valid>; }

  template<typename T, std::enable_if_t<is_valid<T>()>* = nullptr>
    void howdy() const;
};

template<typename T, std::enable_if_t<Hey::is_valid<T>()>*>
  void Hey::howdy() const
{
  std::cout << "Howdy" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

编译器输出:

In file included from hey.cpp:1:
hey.h:18:8: error: no declaration matches ‘void Hey::howdy() const’
   void Hey::howdy() const
        ^~~
hey.h:14:10: note: candidate is: ‘template<class T, …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae c++11

3
推荐指数
1
解决办法
809
查看次数

为什么std :: nullopt_t是C++标准的一部分?

我不明白在标准中包含std :: nullopt_t的原因.它是否仅为方便起见而存在,还是在某些利基环境中需要?

为了清楚起见,我理解它被用作构造空std :: optional对象的参数.但是考虑到std :: optional的默认构造函数已经存在,似乎没有明显的动机存在std :: nullopt_t.对于std :: optional,是否必须存在这样的构造函数和赋值运算符以符合特定的概念?如果是这样,哪个概念?

c++ c++17

0
推荐指数
1
解决办法
410
查看次数

标签 统计

c++ ×2

c++11 ×1

c++17 ×1

sfinae ×1