相关疑难解决方法(0)

C++ 0x中的disable_if在哪里?

Boost有两个enable_ifdisable_if,但C++ 0x似乎缺少后者.为什么遗漏了?在C++ 0x中有元编程工具,让我建立disable_if在以下方面enable_if


哦,我刚刚注意到这std::enable_if基本上boost::enable_if_c就是boost::enable_ifC++ 0x中没有这样的东西.

c++ templates boost enable-if c++11

20
推荐指数
1
解决办法
7662
查看次数

使用不同的enable_if条件选择成员函数

我试图根据类模板参数确定调用哪个版本的成员函数.我试过这个:

#include <iostream>
#include <type_traits>

template<typename T>
struct Point
{
  void MyFunction(typename std::enable_if<std::is_same<T, int>::value, T >::type* = 0)
  {
    std::cout << "T is int." << std::endl;
  }

  void MyFunction(typename std::enable_if<!std::is_same<T, int>::value, float >::type* = 0)
  {
    std::cout << "T is not int." << std::endl;
  }
};

int main()
{
  Point<int> intPoint;
  intPoint.MyFunction();

  Point<float> floatPoint;
  floatPoint.MyFunction();
}
Run Code Online (Sandbox Code Playgroud)

我认为这是说"如果T是int,则使用第一个MyFunction,如果T不是int,则使用第二个MyFunction,但我得到编译器错误"错误:'struct std :: enable_if'中没有类型名为'type'谁能指出我在这里做错了什么?

c++ templates specialization sfinae c++11

15
推荐指数
2
解决办法
1万
查看次数

“struct std::enable_if&lt;false, void&gt; 中没有名为“type”的类型

为什么这不起作用?

它给了我一个错误

error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
     using enable_if_t = typename enable_if<_Cond, _Tp>::type;
Run Code Online (Sandbox Code Playgroud)

这我不把getX()getY()里面struct B它的工作原理。但是,如果我将它们都放在一个结构中,则不会。

error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
     using enable_if_t = typename enable_if<_Cond, _Tp>::type;
Run Code Online (Sandbox Code Playgroud)

但是,这很好用。

#include <iostream>
#include <boost/type_traits/has_dereference.hpp>

struct A {
    A(int i) : x(i) {}
    int x;
};

template<typename T>
struct B {
    template<typename std::enable_if_t<!boost::has_dereference<T>::value> * = nullptr>
    auto getX(T t) {
        return t.x;
    }

    template<typename std::enable_if_t<boost::has_dereference<T>::value> * = nullptr>
    auto getX(T …
Run Code Online (Sandbox Code Playgroud)

c++ type-traits

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

标签 统计

c++ ×3

c++11 ×2

templates ×2

boost ×1

enable-if ×1

sfinae ×1

specialization ×1

type-traits ×1