仅在函数模板中为那些定义了函数的类型执行函数

Execute function inside function template only for those types that have the function defined

本文关键字:函数 类型 执行 定义 那些 函数模板      更新时间:2023-10-16

我有一个函数模板,它采用许多不同的类型作为输入。 在这些类型中,只有一种具有getInt()功能。因此,我希望代码仅针对该类型运行函数。请提出解决方案。谢谢

#include <type_traits>
#include <typeinfo>
class X {
public:
int getInt(){
return 9;
}
};
class Y{
};
template<typename T>
void f(T& v){
// error: 'class Y' has no member named 'getInt'
// also tried std::is_same<T, X>::value 
if(typeid(T).name() == typeid(X).name()){
int i = v.getInt();// I want this to be called for X only
}
}
int main(){
Y y;
f(y);
}

如果您希望能够为具有函数成员getInt的所有类型调用函数f,而不仅仅是X,则可以为函数f声明2个重载:

  1. 对于具有getInt成员函数的类型,包括类X

  2. 对于所有其他类型,包括类Y.

C++11/C++17 解决方案

考虑到这一点,您可以执行以下操作:

#include <iostream>
#include <type_traits>
template <typename, typename = void>
struct has_getInt : std::false_type {};
template <typename T>
struct has_getInt<T, std::void_t<decltype(((T*)nullptr)->getInt())>> : std::is_convertible<decltype(((T*)nullptr)->getInt()), int>
{};
class X {
public:
int getInt(){
return 9;
}
};
class Y {};
template <typename T,
typename std::enable_if<!has_getInt<T>::value, T>::type* = nullptr>
void f(T& v) {
// only for Y
std::cout << "Y" << std::endl;
}
template <typename T,
typename std::enable_if<has_getInt<T>::value, T>::type* = nullptr>
void f(T& v){
// only for X
int i = v.getInt();
std::cout << "X" << std::endl;
}
int main() {
X x;
f(x);
Y y;
f(y);
}

现场查看。

请注意,std::void_t是在 C++17 中引入的,但如果您仅限于 C++11,那么自行实现void_t真的很容易:

template <typename...>
using void_t = void;

这是C++11版本直播。

我们在 C++20 中有什么?

C++20带来了很多好东西,其中之一就是概念。上述适用于 C++11/C++14/C++17 的内容可以在 C++20 中显着减少:

#include <iostream>
#include <concepts>
template<typename T>
concept HasGetInt = requires (T& v) { { v.getInt() } -> std::convertible_to<int>; };
class X {
public:
int getInt(){
return 9;
}
};
class Y {};
template <typename T>
void f(T& v) {
// only for Y
std::cout << "Y" << std::endl;
}
template <HasGetInt T>
void f(T& v){
// only for X
int i = v.getInt();
std::cout << "X" << std::endl;
}
int main() {
X x;
f(x);
Y y;
f(y);
}

现场查看。

您可以使用C++17 中的if constexpr

template<typename T>
void f(T& v){
if constexpr(std::is_same_v<T, X>) { // Or better create trait has_getInt
int i = v.getInt();// I want this to be called for X only
}
// ...
}

在此之前,您必须使用重载和 SFINAE 或标记调度。

保持简单和重载。至少从 C++98 开始工作...

template<typename T>
void f(T& v)
{
// do whatever
}
void f(X& v)
{
int result = v.getInt();
}

如果只有一个类型具有getInt函数,这就足够了。如果还有更多,那就不再那么简单了。有几种方法可以做到这一点,这里有一种:

struct PriorityA { };
struct PriorityB : PriorityA { };
template<typename T>
void f_impl(T& t, PriorityA)
{
// generic version
}
// use expression SFINAE (-> decltype part)
// to enable/disable this overload
template<typename T>
auto f_impl(T& t, PriorityB) -> decltype(t.getInt(), void())
{
t.getInt();
}
template<typename T>
void f(T& t)
{
f_impl(t, PriorityB{ } ); // this will select PriorityB overload if it exists in overload set
// otherwise PriorityB gets sliced to PriorityA and calls generic version
}

带有诊断输出的实时示例。