模板模板从成员函数返回类型
template template return type from member function
如何从成员函数返回任何INT容器?在以下代码中,我尝试了两个测试类之间的Maany排列,但似乎没有使用G -4.8.2:
编译。#include "vector"
struct test {
template<class Container> Container<int> ret() {
return Container<int>();
}
};
struct test {
template<
template<class Int> class Container,
class Int
> typename Container<Int> ret() {
return Container<Int>();
}
};
int main() {
std::vector<int> v = test().ret<std::vector<int> >();
return 0;
}
理想情况下,程序为C 03,并且仅在INT为包含类型时才编译。在其他情况下,向用户打印可读性错误也很不错,但我想这需要Boost或STD static_assert()。谢谢!
编辑1
不幸的是,2个模板参数版本仅适用于几个std容器,评论出来会导致汇编错误,因为它们需要不同的模板参数:
struct test {
template<
template<class, class> class Container
> Container<int, std::allocator<int> > ret() {
return Container<int, std::allocator<int> >();
}
};
int main() {
std::vector<int> v = test().ret<std::vector>();
std::list<int> l = test().ret<std::list>();
//std::set<int> se = test().ret<std::set>();
std::deque<int> d = test().ret<std::deque>();
//std::stack<int> st = test().ret<std::stack>();
//std::queue<int> q = test().ret<std::queue>();
//std::priority_queue<int> p = test().ret<std::priority_queue>();
return 0;
}
但是以下C 11版本似乎适用于每个容器:
struct test {
template<
template<class, class...> class Container,
class... Container_Params
> Container<int, Container_Params... > ret() {
return Container<int, Container_Params... >();
}
};
int main() {
auto v = test().ret<std::vector>();
auto l = test().ret<std::list>();
auto se = test().ret<std::set>();
auto d = test().ret<std::deque>();
auto st = test().ret<std::stack>();
auto q = test().ret<std::queue>();
auto p = test().ret<std::priority_queue>();
auto us = test().ret<boost::unordered_set>();
return 0;
}
test().ret<std::vector, int>();
我认为这是正确的
和..您的test
结构是两个??
template<bool _Test, class _Ty = void>
struct enable_if
{};
template<class _Ty>
struct enable_if<true, _Ty>
{ typedef _Ty type; };
template < typename _Type >
struct is_int_container { static const bool value = false; };
template < typename _Alloc >
struct is_int_container < std::vector < int, _Alloc > > { static const bool value = true; };
template < typename _Alloc >
struct is_int_container < std::list < int, _Alloc > > { static const bool value = true; };
template < typename _Equal, typename _Alloc >
struct is_int_container < std::set < int, _Equal, _Alloc > > { static const bool value = true; };
// Add any required container support
struct test
{
template< typename _Type>
typename enable_if < is_int_container <_Type> ::value, _Type > ::type
ret()
{
return _Type();
}
};
int main(int agrc, char *argv[])
{
test test_;
test_.ret<std::vector<int>>();
test_.ret<std::set<int>>();
test_.ret<std::vector<double>>(); // <- compilator error
return 0;
}
您的问题之一是std::vector
采用2个模板参数(T
和一个分配器)
因此,如果您想参加模板类:
,以下内容可能会为您提供帮助struct test {
template<template<typename, typename> class Container>
Container<int, std::allocator<int>> ret() {
return Container<int, std::allocator<int>>();
}
};
int main(int argc, char *argv[])
{
std::vector<int> v = test().ret<std::vector>();
return 0;
}
使用类的替代方案是使用Sfinae AS:
template <typename Container, typename T>
struct is_container_of :
std::conditional<std::is_same<
typename std::decay<decltype(*std::begin(std::declval<Container>()))>::type,
T>::value,
std::true_type, std::false_type>::type {};
struct test {
template<class Container>
typename std::enable_if<is_container_of<Container, int>::value, Container>::type
ret() {
return Container();
}
};
int main(int argc, char *argv[])
{
auto v = test().ret<std::vector<int>>();
return 0;
}
相关文章:
- 如何获取std::result_of函数的返回类型
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 函数作为模板参数,是否对返回类型强制约束
- C++中函数的向量返回类型引发错误
- 检查函数返回类型是否与STL容器类型值相同
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 为什么 c++(g++) 不允许模板返回类型和函数名称之间有空格?
- 为什么返回类型的'const'限定符对标有 __forceinline/内联的函数没有影响?
- 在 c++ 中将函数返回类型指定为模板参数
- 使用 SWIG 更改生成的 CS 函数中的返回类型
- C++ 这里有一个返回 (24) 的布尔返回类型函数
- 使用SFINAE来检测void返回类型函数的存在
- 模板返回类型函数如何在C++中工作
- 如何在返回类型函数模板的专用化中使用派生类型?( "couldn't infer template argument" )
- Bon appetit :从 int 返回类型函数在 main() 中打印字符串
- 在引用或指针返回类型函数上输入
- 在后面的返回类型函数语法中,auto关键字背后是否有意图
- 我可以在c++中重写字符串返回类型函数吗?