This question already has answers here:
What are some uses of template template parameters?
(10 answers)
Closed 2 years ago.
I have some code:
template<typename Container = std::vector> // there is error
class SomeClass {
struct SomeDataStructure {
int a;
float b;
};
Container<SomeDataStructure> container; // and there
};
I want to pass template class type in template arguments of other template class.
In this example, I want to pass std::vector to SomeClass and define Container(std::vector) of private structure SomeDataStructure.
But I'm getting errors(MSVC compiler) C2059 and C2238.
How can I solve it?
Thanks in advance!
You need template template parameters:
template<template<class, class...> class Container = std::vector>
Related
This question already has answers here:
Partial template specialization for constructor
(1 answer)
how to specialize a template constructor
(1 answer)
Closed 8 months ago.
I'm trying to make a constructor in a templated class act differently if its template parameter is a vector, but am getting an error on the second definition saying "template argument list must match the parameter list". This is what my code looks like:
template<typename T>
class Test
{
Test();
};
template<typename T>
Test<T>::Test() { /* non-vector stuff */ };
template<typename T>
Test<std::vector<T>>::Test() { /* vector stuff */ };
Does anyone know how to do this?
This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 3 years ago.
I'm trying to get this to work:
template<template<typename> class... Attributes>
struct Skills
{
template<class DERIVED>
struct Create : public Attributes<DERIVED>...
{};
};
template <class T, class SKILLS>
class Tester : public typename SKILLS::Create<Tester<T,SKILLS>>
{}
but the compiler complains with:
error C2518: keyword 'typename' illegal in base class list; ignored
This works fine if I don't derive within a class template, however.
Is there any chance to derive from the nested template class?
Why do I need that? I'm trying to implement a nicer interface for a CRTP-class template, which allows doing something like:
using MyClass = ClassWithAttributes<Skills<Plus,Minus,Equal,Clear,Size>>
You have to write
template <class T, class SKILLS>
class Tester : public SKILLS::template Create<Tester<T,SKILLS>>
{ };
I mean
(1) no typename: in the list of base classes is implicit that the arguments are types
(2) you need template, before Create
(3) use SKILLS as argument of Tester, not Skills, because you have declared the template parameter SKILLS
(4) remember the semicolon at the end of class definition
This question already has answers here:
How to know if a type is a specialization of std::vector?
(5 answers)
Closed 3 years ago.
Suppose I have a class
template <typename T>
struct Foo {
T data;
};
where I want to enforce the fact that T is a known templated type. For instance, suppose that I want T to be a std::vector of something. Now one way to handle this problem would be to redefine the class so that the template parameter is also the template parameter of the data function:
template <typename S>
struct Foo {
std::vector<S> data;
};
I do not want to do that. In this specific case, it feels more natural to require the user to construct a foo object like this:
Foo<T<S>> f;
which, in the case where we want T to be a vector, looks like
Foo<std::vector<S>> f;
Is there anyway to keep the first form of the class, which requiring that T be of a specific class? Perhaps a static_assert?
Got it
template <typename T>
struct Foo {
static_assert( std::is_same<T,std::vector<T::value_type>>::value, "T is not a vector you idiot" );
};
This question already has answers here:
Template argument deduction when the function returns a type composed from the template type and another
(2 answers)
Closed 5 years ago.
I have this class:
template<typename Bar, template<typename Bar> class Container>
class Foo
{
Container<Bar> myContainerBar;
};
and this subclass:
class FooSpecialization : public Foo<std::string,std::vector>
{
};
I get the following error at the first line of the second code snippet above (at the subclass):
type/value mismatch at argument 2 in template parameter list for
‘template class Container> class Foo’
I also tried
class FooSpecialization : public Foo<std::string,std::vector<std::string>>
without success. What is wrong? I couldn't find a SO question which addresses this issue.
std::vector has more than one template parameter (Allocator).
You might solve it this way for example
template<typename Bar, template<typename ...> class Container>
class Foo
{
Container<Bar> myContainerBar;
};
class FooSpecialization : public Foo<std::string, std::vector>
{
};
This question already has an answer here:
Wrapping each type in a variadic template in a templated class
(1 answer)
Closed 4 years ago.
I'd like to be able to declare a class with a template pack, such that the class itself will have a member variable tuple that wraps each of the template pack members in a container type of some sort. A basic goal would look like:
template <typename Types...>
class VectorOfMembers
{
public:
// Member tuple where each element is expanded in a container
std::tuple<std::vector<Type[1]>, std::vector<Type[2]>, std::vector<TypeN...>>
};
Ideally I'd like to be able to do this with any templated object as the wrapping type too.
Well, you almost had it:
template <typename... Types>
class VectorOfMembers
{
public:
// Member tuple where each element is expanded in a container
std::tuple<std::vector<Types>...> tuple;
};