gpt4 book ai didi

c++ - SFINAE : Delete a function with the same prototype

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:33 25 4
gpt4 key购买 nike

我想知道这段有效代码之间有什么区别:

#include <type_traits>
#include <iostream>

template<typename T> using is_ref = std::enable_if_t<std::is_reference_v<T>, bool>;
template<typename T> using is_not_ref = std::enable_if_t<!std::is_reference_v<T>, bool>;

template<typename T, is_ref<T> = true>
void foo(T&&) {
std::cout << "ref" << std::endl;
}

template<typename T, is_not_ref<T> = true>
void foo(T&&) {
std::cout << "not ref" << std::endl;
}

int main() {
int a = 0;
foo(a);
foo(5);
}

还有这个不起作用:

#include <type_traits>
#include <iostream>

template<typename T> using is_ref = std::enable_if_t<std::is_reference_v<T>, bool>;
template<typename T> using is_not_ref = std::enable_if_t<!std::is_reference_v<T>, bool>;

template<typename T, typename = is_ref<T>>
void foo(T&&) {
std::cout << "ref" << std::endl;
}

template<typename T, typename = is_not_ref<T>>
void foo(T&&) {
std::cout << "not ref" << std::endl;
}

int main() {
int a = 0;
foo(a);
foo(5);
}

typename = is_ref<T> 之间的真正区别是什么?和 is_ref<T> = true

最佳答案

如果您删除默认的模板参数,就会清楚区别是什么。函数声明不能​​只是它们的默认值不同。这是错误的:

void foo(int i = 4);
void foo(int i = 5);

同样这是错误的:

template <typename T=int> void foo();
template <typename T=double> void foo();

考虑到这一点,您的第一个案例:

template<typename T, is_ref<T>>
void foo(T&&);

template<typename T, is_not_ref<T>>
void foo(T&&);

这里的两个声明是唯一的,因为第二个模板参数在两个声明之间是不同的。第一个有一个非类型模板参数,其类型为 std::enable_if_t<std::is_reference_v<T>, bool>第二个有一个非类型模板参数,其类型为 std::enable_if_t<!std::is_reference_v<T>, bool> .这些是不同的类型。

鉴于,您的第二种情况:

template<typename T, typename>
void foo(T&&);

template<typename T, typename>
void foo(T&&)

这显然是相同的签名 - 但我们只是复制了它。这是错误的。


另见 cppreference's explanation .

关于c++ - SFINAE : Delete a function with the same prototype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50440352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com