std::函数参数类型
std::function argument types
如果你在这里检查,你会发现std::function
应该定义一些可选类型,为了方便起见:
Member types
Type Definition
result_type R
argument_type T if sizeof...(Args)==1 and T is the first and only type in Args...
first_argument_type T1 if sizeof...(Args)==2 and T1 is the first of the two types in Args...
second_argument_type T2 if sizeof...(Args)==2 and T2 is the second of the two types in Args...
如何实现这一要求?我的第一个想法是有条件地继承一个定义两种可能情况的类型的结构,但有没有其他更好的方法,不涉及继承?我对类型擦除或std::function
提供的任何其他功能不感兴趣。
如何根据模板参数专门化数据结构:
template <typename...Args>
struct test
{
};
template <typename Arg1, typename Arg2>
struct test<Arg1, Arg2> //specialization for sizeof...(Args)==2
{
using first_argument_type = Arg1;
using second_argument_type = Arg2;
};
template <typename Arg1>
struct test<Arg1> //specialization for sizeof...(Args)==1
{
using argument_type = Arg1;
};
int main()
{
//test<int, char, std::string>::argument_type t31; //NOK
//test<int, char, std::string>::first_argument_type t32; //NOK
//test<int, char, std::string>::second_argument_type t33; //NOK
//test<int, char>::argument_type t21; //NOK
test<int, char>::first_argument_type t22; //OK
test<int, char>::second_argument_type t23; //OK
test<int>::argument_type t11; //OK
//test<int>::first_argument_type t11; //NOK
//test<int>::second_argument_type t11; //NOK
return 0;
}
至少在Visual C++上,继承是他们的工作方式:
std::function
继承自_Get_function_impl
类_Get_function_impl
类型将_Func_class<_Ret, _Types...>
定义为type
_Func_class
继承自_Fun_class_base
如果没有参数或参数超过2个,则_Fun_class_base
可以条件继承自unary_function
、binary_function
或非。
CCD_ 13和CCD_。
所以是的。。继承是微软团队选择的方式,我不明白为什么不呢?
相关文章:
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 具有可变参数非类型参数的模板专用化
- 函数类型参数的模板参数推导
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 为模板传递非类型参数 agument
- 为什么带有类型参数的运算符 () 可以应用于 result_of 上下文中的类型?
- 使用其他模板类型参数作为要在函数签名中使用的类型别名声明
- 如何避免具有相同类型参数的函数中的错误
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- c++非类型参数包扩展
- 如何实现对参数顺序不可知的std::same_as的广义形式(即对于两个以上的类型参数)
- 在不同的模板参数包之间分发非类型参数包
- 如何在使用容器和字符串时强制使用显式分配器类型参数
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- EXPECT_CALL具有 unique_ptr 引用类型参数的模拟函数
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- C++ 模板:重载时找不到基类类型参数方法