gpt4 book ai didi

c++ - SFINAE 和模板函数实例化 : Why a template argument cannot be deduced when used in function arguments with a SFINAE-enabled type?

转载 作者:行者123 更新时间:2023-11-28 01:45:17 26 4
gpt4 key购买 nike

这些天我正在试验 SFINAE,有些事情让我很困惑。为什么 my_type_a不能在 my_function 中推导出来的实例化?

class my_type_a {};

template <typename T>
class my_common_type {
public:
constexpr static const bool valid = false;
};

template <>
class my_common_type<my_type_a> {
public:
constexpr static const bool valid = true;
using type = my_type_a;
};

template <typename T> using my_common_type_t = typename my_common_type<T>::type;

template <typename T, typename V>
void my_function(my_common_type_t<T> my_cvalue, V my_value) {}

int main(void) {
my_function(my_type_a(), 1.0);
}

G++ 给我这个:

/home/flisboac/test-template-template-arg-subst.cpp: In function ‘int main()’:
/home/flisboac/test-template-template-arg-subst.cpp:21:30: error: no matching function for call to ‘my_function(my_type_a, double)’
my_function(my_type_a(), 1.0);
^
/home/flisboac/test-template-template-arg-subst.cpp:18:6: note: candidate: template<class T, class V> void my_function(my_common_type_t<T>, V)
void my_function(my_common_type_t<T> my_type, V my_value) {}
^~~~~~~~~~~
/home/flisboac/test-template-template-arg-subst.cpp:18:6: note: template argument deduction/substitution failed:
/home/flisboac/test-template-template-arg-subst.cpp:21:30: note: couldn't deduce template parameter ‘T’
my_function(my_type_a(), 1.0);
^

我期望的是,在调用 my_function 时正如我在 main 中所做的那样, T将被推断为函数的第一个参数的类型,并且该类型将在函数的实例化中使用。但似乎 my_common_type_t<T>在函数之前实例化,但即便如此,my_cvalue 的类型会变成my_type_a无论如何,所以我不明白为什么这行不通...

有没有其他方法可以做到这一点?我应该避免两层(或更多层)模板间接寻址吗?

最佳答案

好吧,考虑一下:

template <>
struct my_common_type<int> {
constexpr static const bool valid = true;
using type = my_type_a;
};

template <>
struct my_common_type<double> {
constexpr static const bool valid = true;
using type = my_type_a;
};

// ...

int main(void) {
my_function(my_type_a{}, 1.0);
}

编译器是否选择my_common_type<int>my_common_type<double>

如果语言允许在你的情况下扣除,它必须匹配 T 的内容会在 my_common_type<T>::type为了产生您发送给函数参数的确切类型。显然,这不仅不可能,而且以我上面的例子,它可能有多种选择!

幸运的是,有一种方法可以告诉编译器 my_common_type<T>将始终屈服于 T .技巧的基础是这样的:

template<typename T>
using test_t = T;

template<typename T>
void call(test_t<T>) {}

int main() {
call(1);
}

什么是 T推导出来? int , 简单的!编译器对这种匹配很满意。此外,由于 test_t不能专门化,test_t<soxething>已知仅为 something .

此外,这也适用于多级别名:

template<typename T>
using test_t = T;

template<typename T>
using test2_t = test_t<T>;

template<typename T>
void call(test2_t<T>) {}

int main() {
call(1); // will also work
}

我们可以将其应用于您的案例,但我们需要一些工具:

template<typename T, typename...>
using first_t = T;

这是和上面一样的简单匹配,但是我们也可以发送一些不会被使用的参数。我们将在这个未使用的包中制作 sfinae。

现在,重写my_common_type_t仍然是一个简单的匹配,同时在未使用的包中添加约束:

template <typename T>
using my_common_type_t = first_t<T, typename my_common_type<T>::type>;

请注意,这也有效:

template <typename T>
using my_common_type_t = first_t<T, std::enable_if_t<my_common_type<T>::valid>>;

现在扣除将按预期进行! <知识库> Live (GCC) Live (Clang)

请注意,此技巧仅适用于 C++14,因为这种情况下的 sfinae(丢弃的参数)只能保证在 C++14 之后发生。

另请注意,您应该使用 struct为你的特质,或使用 public:成为成员(member)my_common_type<T>::type public,否则 GCC 将输出虚假错误。

关于c++ - SFINAE 和模板函数实例化 : Why a template argument cannot be deduced when used in function arguments with a SFINAE-enabled type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45406674/

26 4 0