检查第 n 个可变参数模板参数是否属于特定类型
check if the n-th variadic template argument is of a certain type
我想在 C++11 中实现类似 same_type(( 函数的东西,如下所示(这是我到目前为止尝试过的,但它无法应对下面提到的用例(。该函数是检查T
是否与 Args
的第 n 个参数的类型相同,目前 n=0 应该足以满足我的要求,尽管 n=其他有意义的值会更好(如果不是很简单,不是很重要(。
template<typename T, typename... Args>
struct MyClass
{
// check if T is of the same type of the n-th argument of Args
bool same_type() {
// currently, I only check the first argument
// but if possible, it would be more useful to extend
// this function to check the n-th argument
return std::is_same<T,typename std::tuple_element<0, std::tuple<Args...> >::type>;
}
};
我已经看过这个答案,但它没有考虑以下用例。
使用案例:
1.与引用和const
限定符一起使用:
MyClass<int,const int&> c;
// expect to see 1, type of int should match const int&, i.e. reference or const should NOT affect the result
std::cout<<c.same_type();
2.使用时不带参数:
MyClass<int> c;
// expect to see 0 as there is no variadic argument provided to compare with int
std::cout<<c.same_type();
我建议开发一个类型特征isSameNth
如下
template <std::size_t, typename...>
struct isSameNth;
template <std::size_t N, typename T, typename A0, typename ... As>
struct isSameNth<N, T, A0, As...> : public isSameNth<N-1U, T, As...>
{ };
template <std::size_t N, typename T>
struct isSameNth<N, T> : public std::false_type
{ };
template <typename T, typename A0, typename ... As>
struct isSameNth<0U, T, A0, As...> : public std::is_same<
typename std::remove_reference<T>::type const,
typename std::remove_reference<A0>::type const>
{ };
在模板静态方法中转换same_type()
(其中模板值为 N
(
template <typename T, typename... Args>
struct MyClass
{
template <std::size_t N>
static constexpr bool same_type()
{ return isSameNth<N, T, Args...>::value; }
};
以下是完整示例(符合 C++11(
#include <type_traits>
template <std::size_t, typename...>
struct isSameNth;
template <std::size_t N, typename T, typename A0, typename ... As>
struct isSameNth<N, T, A0, As...> : public isSameNth<N-1U, T, As...>
{ };
template <std::size_t N, typename T>
struct isSameNth<N, T> : public std::false_type
{ };
template <typename T, typename A0, typename ... As>
struct isSameNth<0U, T, A0, As...> : public std::is_same<
typename std::remove_reference<T>::type const,
typename std::remove_reference<A0>::type const>
{ };
template <typename T, typename... Args>
struct MyClass
{
template <std::size_t N>
static constexpr bool same_type()
{ return isSameNth<N, T, Args...>::value; }
};
int main ()
{
static_assert(
false == MyClass<int, long, int, short>::template same_type<0U>(), "!");
static_assert(
true == MyClass<int, long, int, short>::template same_type<1U>(), "!");
static_assert(
false == MyClass<int, long, int, short>::template same_type<2U>(), "!");
static_assert(
true == MyClass<int const, int &>::template same_type<0U>(), "!");
static_assert(
false == MyClass<int const &>::template same_type<0U>(), "!");
}
我认为您更希望检查兼容性而不是"相同性"。 这是一种方法:
#include <tuple>
#include <type_traits>
#include <iostream>
#include <string>
template<class...Ts>
struct nth_is_compatible
{
using tuple = std::tuple<Ts...>;
template<class T, std::size_t N> static constexpr bool check()
{
return std::is_convertible<decltype(std::get<N>(std::declval<tuple>())), T>::value;
}
};
struct Monkey
{
Monkey(std::string) {} // conversion constructor
};
int main()
{
using checklist = nth_is_compatible<const int&, float, std::string>;
constexpr auto list = checklist();
std::cout << list.check<int, 0>() << std::endl;
std::cout << list.check<int, 1>() << std::endl;
std::cout << list.check<int, 2>() << std::endl;
// prove it's a constexpr and that it works for conversions
constexpr auto monkeyable = checklist::check<Monkey, 2>();
std::cout << monkeyable << std::endl;
}
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 隐式可转换参数,但属于引用类型
- 通过可变参数模板应用属于其他类的函数
- 仅当数组属于特定类型时,才将模板化参数添加到数组
- 检查第 n 个可变参数模板参数是否属于特定类型
- 根据C++中的输入参数使对象属于一个类
- 如何让一个函数参数接受不同的对象(但属于同一类),并访问传递给w/n函数调用的w/n对象的函数