相关疑难解决方法(0)

SFINAE的问题

为什么这个代码(M类中的fnc值)不能通过SFINAE规则解决?我收到一个错误:

Error   1   error C2039: 'type' : is not a member of
                                   'std::tr1::enable_if<_Test,_Type>'  
Run Code Online (Sandbox Code Playgroud)

当然类型不是成员,它没有在enable_if的这个通用版本中定义,但是如果bool为真,并且如果它是假的则不实例化那么这不是fnc的这个版本背后的全部想法吗?可以请有人向我解释一下吗?

#include <iostream>
#include <type_traits>

using namespace std;

template <class Ex> struct Null;
template <class Ex> struct Throw;

template <template <class> class Policy> struct IsThrow;

template <> struct IsThrow<Null> {
    enum {value = 0};
};

template <> struct IsThrow<Throw> {
    enum {value = 1};
};

template <template <class> class Derived>
struct PolicyBase {
    enum {value = IsThrow<Derived>::value};
};

template<class Ex>
struct Null : PolicyBase<Null> { };

template<class …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae

5
推荐指数
1
解决办法
848
查看次数

在C++中减少模板化的类参数

我有一个这样的课:

template <unsigned int A, unsigned int B>
class Foo { ... };
Run Code Online (Sandbox Code Playgroud)

Foo需要一个名为bar()的方法,但我需要专门化它.对于一个案例,当A == B时,我希望它做一件事,否则就是其他事情.如果不在函数中编写if语句,我可以这样做吗?喜欢:

Foo<A, A>::bar() { ... } and Foo<A, B>::bar() { ... }
Run Code Online (Sandbox Code Playgroud)

c++ templates

4
推荐指数
1
解决办法
79
查看次数

无法使用 SFINAE (std::enable_if) 覆盖虚函数

下面的代码virtual A::foo()B::foo() override. 如果BROKEN没有定义,那么它编译就好了。我对std::enable<std::is_pod<Q>::value>::type恶作剧的理解是它有效地替换了 SFINAE 表达式void,这应该与普通的void.

但是,它不会编译。我收到编译器错误:

英特尔编译器(icpc):

$ make a CXXFLAGS="-std=c++11 -DBROKEN"
icpc -std=c++11 -DBROKEN    a.cpp   -o a
a.cpp(24): error: object of abstract class type "B<int>" is not allowed:
            pure virtual function "A::foo" has no overrider
     B<int> b;
            ^
Run Code Online (Sandbox Code Playgroud)

海湾合作委员会:

$ make a CXXFLAGS="-std=c++11 -DBROKEN" CXX=g++
g++ -std=c++11 -DBROKEN    a.cpp   -o a
a.cpp: In function ‘int main()’:
a.cpp:24:11: error: cannot declare variable ‘b’ to be of abstract …
Run Code Online (Sandbox Code Playgroud)

virtual sfinae c++11

4
推荐指数
1
解决办法
1900
查看次数

类定义中的条件

我正在尝试做类似的事情:

#pragma once
#include <memory>
#include <type_traits>
#include <vector>

class B{}

template <class T>
class A
{
    private:
        std::vector<std::shared_ptr<T>> ptrVector;
    public:
        A<T>();
        void pushBack(std::shared_ptr<T> t);
        if(std::is_same<T, B>::value)
        {
            void doSth();
        }
        ~A<T>(){};
};
Run Code Online (Sandbox Code Playgroud)

不管怎么说,甚至有可能做到这样的条件吗?不,我不能继承这个类,并且只有A<B>doSth()不存在时才需要doSth()A<C>.

c++ oop templates class conditional-statements

4
推荐指数
1
解决办法
109
查看次数

限制访问功能

我有一个带有函数的泛型类,我想在编译时将其限制为浮点类型的实例.如下例所示:

template <typename T>
class ClassName
{
    // instance variables, etc..

    void some_method()
    {
        // do stuff, but only for floating point types
    }
}
Run Code Online (Sandbox Code Playgroud)

如何让编译器拒绝some_method用于非浮点类型的ClassName?

我一直在看SFINAE,但我根本无法让它工作,所以经过几个小时的失败后,我正在寻求你的帮助.

谢谢 :)

c++ traits type-traits c++11

3
推荐指数
2
解决办法
285
查看次数

如何使用 SFINAE 从 C++11 中的多个选项中选择构造函数

我的问题是这个问题的扩展: 如何使用 sfinae 来选择构造函数?

在上一个问题中,提问者只是想有选择地启用单个构造函数。我想根据类模板参数类型是否可默认构造来更改构造函数的行为 - 我能想出的最好方法是让两个构造函数具有相同的用法,以便只启用一个对于每个实例化。我的情况也不同,因为如果我不尝试有选择地启用 with enable_if(而在链接的问题中,构造函数的两个版本都是模板化的)int otherN)。

上述问题的已接受答案中的评论将我带到了此站点,这使我创建了以下最小示例:

#include <iostream>
#include <type_traits>

namespace detail {
    enum class enabler {};
    enum class disabler {};
}

template <typename Condition>
using EnableIf = typename std::enable_if<Condition::value, detail::enabler>::type;

template <typename Condition>
using DisableIf = typename std::enable_if<!Condition::value, detail::disabler>::type;

template<typename T>
struct A {

    T data;

    // Valid if T is default-construtible; SFINAE otherwise
    template<EnableIf<std::is_default_constructible<T>>...>
    A() { std::cout << "Data defaulted" << std::endl; }


    // Valid if T is …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae c++11

3
推荐指数
2
解决办法
903
查看次数

在 C++ 类中有条件地启用构造函数

我正在学习如何使用std::enable_if,到目前为止,我在有条件地启用和禁用课程中的方法方面取得了一定程度的成功。我针对布尔值对方法进行模板化,并且此类方法的返回类型是std::enable_if此类布尔值。这里的最小工作示例:

#include <array>
#include <iostream>
#include <type_traits>

struct input {};
struct output {};

template <class io> struct is_input { static constexpr bool value = false; };

template <> struct is_input<input> { static constexpr bool value = true; };

template <class float_t, class io, size_t n> class Base {
private:
  std::array<float_t, n> x_{};

public:
  Base() = default;
  Base(std::array<float_t, n> x) : x_(std::move(x)) {}
  template <class... T> Base(T... list) : x_{static_cast<float_t>(list)...} {}

  // Disable the getter if it …
Run Code Online (Sandbox Code Playgroud)

c++ constructor enable-if

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

std :: enable_if用于两种不同的方法实现(4种不同的情况)

我需要为const和非const类型实现两种不同的方法.我已经设法编写工作代码,但我不明白为什么它的一些味道是好的,而其中一些不是.

这是一个简化的例子,我想知道为什么#1有效,但#2不是,和#3对#4相同:

#include <iostream>
#include <vector>

template <typename T>
class X {
public:
    // #1 - works
    template<typename B = T, typename std::enable_if<std::is_const<B>::value, int>::type = 0>
    void foo() {std::cout << "CONST" << std::endl;}
    template<typename B = T, typename std::enable_if<std::is_const<B>::value == false, int>::type = 0>
    void foo() {std::cout << "NON-CONST" << std::endl;}

    // #2 - does not work "no type named 'type' in 'std::__1::enable_if<false, int>'; 'enable_if' cannot be used to disable this declaration"
//    template<typename std::enable_if<std::is_const<T>::value, int>::type = 0>
//    void …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

std :: enable_if更改成员* variable *声明/类型

我看了几个类似的问题,例如这个问题和另一个问题,并且我了解了如何针对成员函数使用enable_if 。

这是一个工作示例:

#include <iostream>

template <int size>
class Test
{
private:
    constexpr static bool ENABLE = (size < 10);

public:
    template <bool E = ENABLE, typename std::enable_if<E, int>::type = 0>
    static int foo();

    template <bool E = ENABLE, typename std::enable_if<!E, int>::type = 0>
    constexpr static int foo();
};

template <int size>
template <bool E, typename std::enable_if<E, int>::type>
int Test<size>::foo()
{
    return 7;
}

template <int size>
template <bool E, typename std::enable_if<!E, int>::type>
constexpr …
Run Code Online (Sandbox Code Playgroud)

c++ templates enable-if

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

C++中的编译时条件成员函数调用

我有一个模板类,如果模板参数满足某些条件,某些成员函数才有意义.例如,使用std::enable_if<>我只能为这些情况定义它们,但我怎样才能有条件地调用它们?这是一个简短的例子

template<class T> class A
{
   typename std::enable_if<std::is_floating_point<T>::value>::type a_member();
   void another_member()
   {
     a_member(); // how to restrict this to allowed cases only?
   }
};
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

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