如何使用SFINAE禁用方法重载集?

问题描述 投票:1回答:2

我正在编写一个有两个参数的模板类。这个类还包含一个带有两个重载的方法,它接受一个类模板参数指定的类型的值,并返回另一个模板参数指定的类型的值,如下所示:

// Type1 and Type2 are class template arguments
Type2 overloadedMethod(Type1);
Type1 overloadedMethod(Type2);

当模板参数不同时,这显然有效,但当它们相同时会成为问题,因为这会导致两个具有相同签名的重载。因此,如果参数相同,我只想禁止使用两个重载,以便代码可以编译。

到目前为止,我试图使用SFINAE和std::enable_if解决这个问题,但没有取得多大成功。我目前的实现如下:

template <typename Type1, typename Type2>
class OverloadDisablingClass
{
  public:
    template <typename MethodType1 = Type1, typename MethodType2 = Type2,
              std::enable_if_t<!std::is_same<MethodType1, MethodType2>::value, bool> = true>
    Type2 overloadedMethod(Type1);
    template <typename MethodType1 = Type1, typename MethodType2 = Type2,
              std::enable_if_t<!std::is_same<MethodType1, MethodType2>::value, bool> = true>
    Type1 overloadedMethod(Type2);
};

我试图像这样使用它:

// attempt to disable set of overloaded methods:
OverloadDisablingClass<int, std::string> overloadClassWithFullApi;
overloadClassWithFullApi.overloadedMethod(1);
overloadClassWithFullApi.overloadedMethod("one");
OverloadDisablingClass<std::string, std::string> overloadClassWithPartialApi;
// above line doesn't compile, overloads with same signature

不幸的是,当我尝试编译程序时,gcc在我创建具有相同模板参数的类的实例的行中失败并出现以下错误:

error: 'template<class MethodType1, class MethodType2, typename std::enable_if<(! std::is_same<MethodType1, MethodType2>::value), bool>::type <anonymous> > Type1 OverloadDisablingClass<Type1, Type2>::overloadedMethod(Type2) [with MethodType1 = MethodType1; MethodType2 = MethodType2; typename std::enable_if<(! std::is_same<MethodType1, MethodType2>::value), bool>::type <anonymous> = <enumerator>; Type1 = std::__cxx11::basic_string<char>; Type2 = std::__cxx11::basic_string<char>]' cannot be overloaded
 Type1 OverloadDisablingClass<Type1, Type2>::overloadedMethod(Type2)
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: with 'template<class MethodType1, class MethodType2, typename std::enable_if<(! std::is_same<MethodType1, MethodType2>::value), bool>::type <anonymous> > Type2 OverloadDisablingClass<Type1, Type2>::overloadedMethod(Type1) [with MethodType1 = MethodType1; MethodType2 = MethodType2; typename std::enable_if<(! std::is_same<MethodType1, MethodType2>::value), bool>::type <anonymous> = <enumerator>; Type1 = std::__cxx11::basic_string<char>; Type2 = std::__cxx11::basic_string<char>]'
 Type2 OverloadDisablingClass<Type1, Type2>::overloadedMethod(Type1)
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

除非我弄错了,否则这两个重载都会被编译器评估,尽管我试图禁用它们,当然,由于两者都被评估为相同的签名,编译失败了。

起初,我认为可能用于禁用这些方法的技术有点瑕疵,因为我在使用SFINAE时还没有经验,所以我尝试了一个更简单的例子,其中我有一个非重载的类根据类的单个模板参数禁用的方法。但是,我能够成功编译生成的代码,如果满足条件,则该方法将被禁用。

我的示例的完整源代码用于测试行为:

// main.cpp

#include "methoddisablingclass.hpp"
#include "overloaddisablingclass.hpp"

#include <string>

int main()
{
    // attempt to disable set of overloaded methods:
    OverloadDisablingClass<int, std::string> overloadClassWithFullApi;
    overloadClassWithFullApi.overloadedMethod(1);
    overloadClassWithFullApi.overloadedMethod("one");
    OverloadDisablingClass<std::string, std::string> overloadClassWithPartialApi;
    // above line doesn't compile, overloads with same signature

    // simpler attempt to test the SFINAE, disables a single method, works as expected
    MethodDisablingClass<std::string> simpleClassWithFullApi;
    simpleClassWithFullApi.alwaysSuitableMethod();
    simpleClassWithFullApi.sometimesUnsuitableMethod();
    MethodDisablingClass<int> simpleClassWithPartialApi;
    simpleClassWithPartialApi.alwaysSuitableMethod();
    // simpleClassWithPartialApi.sometimesUnsuitableMethod(); // <- fails to compile, as is intended

    return 0;
}
// methoddisablingclass.hpp

#ifndef METHODDISABLINGCLASS_HPP
#define METHODDISABLINGCLASS_HPP

#include <iostream>
#include <type_traits>

template <typename Type>
class MethodDisablingClass
{
  public:
    template <typename MethodType = Type, std::enable_if_t<!std::is_integral<MethodType>::value, bool> = true>
    void sometimesUnsuitableMethod();
    void alwaysSuitableMethod();
};

template <typename Type>
template <typename MethodType, std::enable_if_t<!std::is_integral<MethodType>::value, bool>>
void MethodDisablingClass<Type>::sometimesUnsuitableMethod()
{
    std::cout << __func__ << "\n";
}

template <typename Type>
void MethodDisablingClass<Type>::alwaysSuitableMethod()
{
    std::cout << __func__ << "\n";
}

#endif  // METHODDISABLINGCLASS_HPP
// overloaddisablingclass.hpp

#ifndef OVERLOADDISABLINGCLASS_HPP
#define OVERLOADDISABLINGCLASS_HPP

#include <iostream>
#include <type_traits>

template <typename Type1, typename Type2>
class OverloadDisablingClass
{
  public:
    template <typename MethodType1 = Type1, typename MethodType2 = Type2,
              std::enable_if_t<!std::is_same<MethodType1, MethodType2>::value, bool> = true>
    Type2 overloadedMethod(Type1);
    template <typename MethodType1 = Type1, typename MethodType2 = Type2,
              std::enable_if_t<!std::is_same<MethodType1, MethodType2>::value, bool> = true>
    Type1 overloadedMethod(Type2);
};

template <typename Type1, typename Type2>
template <typename MethodType1, typename MethodType2, std::enable_if_t<!std::is_same<MethodType1, MethodType2>::value, bool>>
Type2 OverloadDisablingClass<Type1, Type2>::overloadedMethod(Type1)
{
    std::cout << __func__ << " for Type1\n";
    return Type2();
}

template <typename Type1, typename Type2>
template <typename MethodType1, typename MethodType2, std::enable_if_t<!std::is_same<MethodType1, MethodType2>::value, bool>>
Type1 OverloadDisablingClass<Type1, Type2>::overloadedMethod(Type2)
{
    std::cout << __func__ << " for Type2\n";
    return Type1();
}

#endif  // OVERLOADDISABLINGCLASS_HPP

因此,我想请求帮助解决这个问题。具体来说,我想知道为什么我当前的代码不起作用,我如何使用SFINAE来禁用模板化类中的一组重载方法,具体取决于此类的模板参数。

我需要一个兼容C ++ 14的解决方案,但是如果对依赖于C ++ 17(或C ++ 20?:-))的解决方案有可能的改进,那么我也会对它们感兴趣。

请注意,在使用SFINAE无法执行此操作后,我开始考虑使用普通的旧模板特化,并使用继承来避免代码重复(我想要使用它的实际类比一个更复杂的类)例)。但是这个问题引起了我的兴趣所以我想知道解决方案,无论我最终使用哪种方法。

c++ templates c++14 overloading sfinae
2个回答
1
投票

我认为你的重载问题是它们根本不依赖于模板参数。任何不依赖于模板参数的东西必须是有效的,无论替换是什么 - 即使在稍后阶段它被拒绝并且不能被使用。

(注意,我指的是方法的模板参数(MethodType1MethodType2),而不是捕获的模板类型Type1Type2

快速修复使签名依赖于方法的模板参数似乎对我有用:

#include <type_traits>
#include <string>

template <typename Type1, typename Type2>
class OverloadDisablingClass
{
  public:
    template <typename MethodType1 = Type1, typename MethodType2 = Type2, std::enable_if_t<!std::is_same<MethodType1, MethodType2>::value, bool> = true>
    MethodType2 overloadedMethod(Type1) {}
    template <typename MethodType1 = Type1, typename MethodType2 = Type2, std::enable_if_t<!std::is_same<MethodType1, MethodType2>::value, bool> = true>
    MethodType1 overloadedMethod(Type2) {}
};

int main() {
    OverloadDisablingClass<int, std::string> overloadClassWithFullApi;
    overloadClassWithFullApi.overloadedMethod(1);
    overloadClassWithFullApi.overloadedMethod("one");
    OverloadDisablingClass<std::string, std::string> overloadClassWithPartialApi;
    return 0;
}

0
投票

从不同角度处理问题的一种方法是在获得两个相同参数的情况下专门化OverloadDisablingClass

template <typename Type1, typename Type2>
class OverloadDisablingClass
{
  public:
    Type2 overloadedMethod(Type1);
    Type1 overloadedMethod(Type2);
};

// Implement methods here

template <typename Type1>
class OverloadDisablingClass<Type1, Type1>
{
  public:
    Type1 overloadedMethod(Type1);
};

// Implement specialization here
© www.soinside.com 2019 - 2024. All rights reserved.