Как я могу вернуть любой контейнер из int из функции-члена? В следующем коде я попытался maany перестановки между двумя тестовыми классами, но ничего, кажется, компилировать с помощью г ++ - 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 и компилировать только если ИНТ является . Печатать читаемую ошибку для пользователя в других случаях также было бы неплохо, но я предполагаю, что для этого потребуется либо boost, либо std static_assert(). Благодаря!
EDIT 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;
}
'std :: vector' is no t тип – tumdum