派生 class 和 std::is_base_of 的 C++ 模板函数
C++ template function for derived class with std::is_base_of
我在为给定类型创建函数时遇到问题,如果它派生自其他类型,则执行某些操作,而对于所有其他情况,则执行其他操作。我的代码:
class BaseClass {};
class DerivedClass : public BaseClass {};
template <typename T>
void Function(typename std::enable_if<std::is_base_of<BaseClass, T>::value, T>::type && arg) {
std::cout << "Proper";
}
template <typename T>
void Function(T && arg) {
std::cout << "Improper";
}
void test() {
Function(DerivedClass{});
}
对于 class DeriviedClass
和其他基于 BaseClass
我想调用函数 couting Proper
,但是它 couts Improper
。有什么建议吗?
如问题评论中所述,SFINAE 表达式不会像您那样工作。
它应该是这样的:
template <typename T>
typename std::enable_if<std::is_base_of<BaseClass, T>::value>::type
Function(T && arg) {
std::cout << "Proper" << std::endl;
}
template <typename T>
typename std::enable_if<not std::is_base_of<BaseClass, T>::value>::type
Function(T && arg) {
std::cout << "Improper" << std::endl;
}
SFINAE 表达式将启用或禁用 [=11=],具体取决于 BaseClass
是 T
的基数这一事实。 Return 类型在这两种情况下都是 void
,因为如果您不定义它,它是 std::enable_it
的默认类型。
在 coliru.
上查看
存在其他有效的替代方案,其中一些已在其他答案中提及。
template <typename T>
auto Function(T && arg) -> typename std::enable_if<std::is_base_of<BaseClass, T>::value>::type
{
std::cout << "Proper";
}
template <typename T>
auto Function(T && arg) -> typename std::enable_if<!std::is_base_of<BaseClass, T>::value>::type
{
std::cout << "Improper";
}
#include <typeinfo>
#include <iostream>
class BaseClass {};
class DerivedClass : public BaseClass {};
class OtherClass {};
template <typename T,typename = typename std::enable_if<std::is_base_of<BaseClass, T>::value, T>::type>
void Function(T && arg)
{
std::cout << "Proper" << std::endl;
}
void Function(...)
{
std::cout << "Improper"<< std::endl;
}
int main()
{
Function(DerivedClass{});
Function(BaseClass{});
Function(OtherClass{});
}
C++11+ :
#include <type_traits> // for is_base_of<>
class Base {};
class Derived : public Base {};
class NotDerived {};
template<typename Class>
void foo(const Class& cls)
{
static_assert(is_base_of<Base, Class>::value, "Class doesn't inherit from Base!");
// The codes...
}
int main()
{
foo(Derived()); // OK!
foo(NotDerived()); // Error!
return 0;
}
我在为给定类型创建函数时遇到问题,如果它派生自其他类型,则执行某些操作,而对于所有其他情况,则执行其他操作。我的代码:
class BaseClass {};
class DerivedClass : public BaseClass {};
template <typename T>
void Function(typename std::enable_if<std::is_base_of<BaseClass, T>::value, T>::type && arg) {
std::cout << "Proper";
}
template <typename T>
void Function(T && arg) {
std::cout << "Improper";
}
void test() {
Function(DerivedClass{});
}
对于 class DeriviedClass
和其他基于 BaseClass
我想调用函数 couting Proper
,但是它 couts Improper
。有什么建议吗?
如问题评论中所述,SFINAE 表达式不会像您那样工作。
它应该是这样的:
template <typename T>
typename std::enable_if<std::is_base_of<BaseClass, T>::value>::type
Function(T && arg) {
std::cout << "Proper" << std::endl;
}
template <typename T>
typename std::enable_if<not std::is_base_of<BaseClass, T>::value>::type
Function(T && arg) {
std::cout << "Improper" << std::endl;
}
SFINAE 表达式将启用或禁用 [=11=],具体取决于 BaseClass
是 T
的基数这一事实。 Return 类型在这两种情况下都是 void
,因为如果您不定义它,它是 std::enable_it
的默认类型。
在 coliru.
存在其他有效的替代方案,其中一些已在其他答案中提及。
template <typename T>
auto Function(T && arg) -> typename std::enable_if<std::is_base_of<BaseClass, T>::value>::type
{
std::cout << "Proper";
}
template <typename T>
auto Function(T && arg) -> typename std::enable_if<!std::is_base_of<BaseClass, T>::value>::type
{
std::cout << "Improper";
}
#include <typeinfo>
#include <iostream>
class BaseClass {};
class DerivedClass : public BaseClass {};
class OtherClass {};
template <typename T,typename = typename std::enable_if<std::is_base_of<BaseClass, T>::value, T>::type>
void Function(T && arg)
{
std::cout << "Proper" << std::endl;
}
void Function(...)
{
std::cout << "Improper"<< std::endl;
}
int main()
{
Function(DerivedClass{});
Function(BaseClass{});
Function(OtherClass{});
}
C++11+ :
#include <type_traits> // for is_base_of<>
class Base {};
class Derived : public Base {};
class NotDerived {};
template<typename Class>
void foo(const Class& cls)
{
static_assert(is_base_of<Base, Class>::value, "Class doesn't inherit from Base!");
// The codes...
}
int main()
{
foo(Derived()); // OK!
foo(NotDerived()); // Error!
return 0;
}