使用函数重载解决方案的概念(而不是SFINAE)

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

试图向SFINAE说再见。

是否可以使用concepts来区分功能,所以编译器可以根据发送的参数是否满足concept约束来匹配正确的功能?

例如,重载这两个:

// (a)
void doSomething(auto t) { /* */ }

// (b)
void doSomething(ConceptA auto t) { /* */ }

因此,在被调用时,编译器将在每个调用中匹配正确的函数:

doSomething(param_doesnt_adhere_to_ConceptA); // calls (a)
doSomething(param_adheres_to_ConceptA); // calls (b)

相关问题:Will Concepts replace SFINAE?,但由于C ++ 20标准现在(几乎)是最终的,正在寻找确切的和更新的代码示例。

c++ sfinae overload-resolution c++20 c++-concepts
1个回答
0
投票

concepts是为此目的而设计的。如果发送的参数不符合所需的概念参数,则不会在重载解决方案列表中考虑该函数,从而避免了歧义。

此外,如果发送的参数满足多个功能,则将选择更具体的一个。

简单示例:

void print(auto t) {
    std::cout << t << std::endl;
}

void print(std::integral auto i) {
    std::cout << "integral: " << i << std::endl;
}

print函数以上是可以一起存在的有效重载。

  • 如果我们发送非整数类型,它将选择第一个
  • 如果我们发送整数类型,它将首选第二种类型

例如,调用函数:

print("hello"); // calls print(auto)
print(7);       // calls print(std::integral auto)

无歧义-这两个功能可以完美地并排在一起。

不需要任何SFINAE代码,例如enable_if-它已被应用(很好地隐藏了)。


在两个概念之间选择

上面的示例显示了编译器相对于无约束类型(just auto)相对于无约束类型(std :: integral auto)的偏好。但是,规则也适用于两个相互竞争的概念。如果更具体,则编译器应选择更具体的一个。当然,如果同时满足这两个概念,并且没有一个更具体,则会导致模棱两可。

嗯,是什么使概念更具体?如果它是基于另一个。

通用概念-GenericTwople

template<class P>
concept GenericTwople = requires(P p) {
    requires std::tuple_size<P>::value == 2;
    std::get<0>(p);
    std::get<1>(p);
};

更具体的概念-双向:

class Any;

template<class Me, class TestAgainst>
concept type_matches =
    std::same_as<TestAgainst, Any> ||
    std::same_as<Me, TestAgainst>  ||
    std::derived_from<Me, TestAgainst>;

template<class P, class First, class Second>
concept Twople =
    GenericTwople<P> && // <= note this line
    type_matches<std::tuple_element_t<0, P>, First> &&
    type_matches<std::tuple_element_t<1, P>, Second>;

请注意,Twople必须满足GenericTwople的要求,因此更具体。

如果您在我们的Twople中替换行:

    GenericTwople<P> && // <= note this line

根据此行带来的实际需求,Twople仍将具有相同的需求,但将不再比GenericTwople更具体。当然,这与代码重用一起,也是为什么我们倾向于根据GenericTwople要求定义Twople的原因。


现在我们可以处理各种重载:

void print(auto t) {
    std::cout << t << std::endl;
}

void print(const GenericTwople auto& p) {
    std::cout << "GenericTwople: " << std::get<0>(p) << ", " << std::get<1>(p) << std::endl;
}

void print(const Twople<int, int> auto& p) {
    std::cout << "{int, int}: " << std::get<0>(p) << ", " << std::get<1>(p) << std::endl;
}

并使用:

print(std::tuple{1, 2});        // goes to print(Twople<int, int>)
print(std::tuple{1, "two"});    // goes to print(GenericTwople)
print(std::pair{"three", 4});   // goes to print(GenericTwople)
print(std::array{5, 6});        // goes to print(Twople<int, int>)
print("hello");                 // goes to print(auto)

我们可以走得更远,因为上面介绍的Twople概念也适用于多态性:

struct A{
    virtual ~A() = default;
    virtual std::ostream& print(std::ostream& out = std::cout) const {
        return out << "A";
    }
    friend std::ostream& operator<<(std::ostream& out, const A& a) {
        return a.print(out);
    }
};

struct B: A{
    std::ostream& print(std::ostream& out = std::cout) const override {
        return out << "B";
    }
};

添加以下重载:

void print(const Twople<A, A> auto& p) {
    std::cout << "{A, A}: " << std::get<0>(p) << ", " << std::get<1>(p) << std::endl;
}

并使用:](并在所有其他重载仍然存在的情况下调用它)

    print(std::pair{B{}, A{}}); // calls the specific print(Twople<A, A>)

代码:https://godbolt.org/z/dFSg8o


不幸的是,C ++ 20不允许概念专业化,否则,我们将走得更远,带有:

template<class P>
concept Twople<Any, Any> = GenericTwople<P>;

这可以为this SO question添加一个很好的可能答案,但是不允许概念专门化。

© www.soinside.com 2019 - 2024. All rights reserved.