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

How to disable set of method overloads using SFINAE?

本文关键字:重载 方法 何使用 SFINAE      更新时间:2023-10-16

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

// 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 Type1n";
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 Type2n";
return Type1();
}
#endif  // OVERLOADDISABLINGCLASS_HPP

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

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

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

我认为重载的问题在于它们根本不依赖于模板参数。无论替换如何,任何不依赖于模板参数的内容都必须有效 - 即使在稍后阶段它被拒绝并且无法使用。

(注意,我指的是方法的模板参数(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;
}

从不同角度处理问题的一种方法是专门针对获得两个相同参数的情况进行专门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