Reputation: 358
In C++ (14) I have a template class with a type T for which I would like to use a class as the type. The class that I want to use for the type requires a parameter in its constructor... so how do I pass this parameter through the template instantiation?
My example code (template_hell.cpp):
#include <iostream>
template <typename T>
class my_template
{
public:
struct Stored_Data_Type
{
T data;
int set_count = 0;
};
my_template() : m_data() {};
T& write()
{
m_data.set_count++;
return m_data.data;
}
const T& get() const {return m_data.data;}
private:
Stored_Data_Type m_data;
};
class a_class
{
public:
a_class(int init): m_data(init) {};
void set(const int data) {m_data = data;};
const int get() const {return m_data;};
private:
int m_data;
};
class b_class : public a_class
{
public:
b_class(): a_class{0} {};
};
int main()
{
//a_class b(1);
b_class b;
b.set(2);
std::cout << "b: " << b.get() << std::endl;
my_template<int> my_int;
my_int.write() = 10;
std::cout << "my_int: " << my_int.get() << std::endl;
my_template<b_class> my_b;
my_b.write().set(2);
std::cout << "my_b: " << my_b.get().get() << std::endl;
// Compile error here:
my_template<a_class> my_a;
my_a.write().set(3);
std::cout << "my_a: " << my_a.get().get() << std::endl;
}
This all works and is fine until I add in the my_a
template instance. Then I get this compile error:
template_hell.cpp: In instantiation of 'my_template<T>::my_template() [with T = a_class]':
template_hell.cpp:62:24: required from here
template_hell.cpp:13:26: error: use of deleted function 'my_template<a_class>::Stored_Data_Type::Stored_Data_Type()'
my_template() : m_data() {};
^
template_hell.cpp:7:10: note: 'my_template<a_class>::Stored_Data_Type::Stored_Data_Type()' is implicitly deleted because the default definition would be ill-formed:
struct Stored_Data_Type
^~~~~~~~~~~~~~~~
template_hell.cpp:7:10: error: no matching function for call to 'a_class::a_class()'
template_hell.cpp:31:3: note: candidate: a_class::a_class(int)
a_class(int init): m_data(init) {};
^~~~~~~
template_hell.cpp:31:3: note: candidate expects 1 argument, 0 provided
template_hell.cpp:27:7: note: candidate: constexpr a_class::a_class(const a_class&)
class a_class
^~~~~~~
template_hell.cpp:27:7: note: candidate expects 1 argument, 0 provided
template_hell.cpp:27:7: note: candidate: constexpr a_class::a_class(a_class&&)
template_hell.cpp:27:7: note: candidate expects 1 argument, 0 provided
How do I pass the parameter to a_class through the template instance? Is this not possible?
Upvotes: 1
Views: 224
Reputation: 4096
You could make use of Variadic arguments. See https://en.cppreference.com/w/cpp/language/parameter_pack. This allows us to forward on any arguments to the StoredDataType
that are passed to our my_template
class
Variadic templates allow us to write classes/methods that take an arbitrary number of arguments in a type-safe way and have all the argument handling logic resolved at compile-time, rather than run-time
#include <iostream>
template <typename T>
class my_template
{
public:
struct Stored_Data_Type
{
template<typename... TArgs>
Stored_Data_Type(TArgs&&... args) : data(std::forward<TArgs>(args)...)
{
}
T data;
int set_count = 0;
};
template<typename... TArgs>
my_template(TArgs&&... args) : m_data(std::forward<TArgs>(args)...) {}
T& write()
{
m_data.set_count++;
return m_data.data;
}
const T& get() const {return m_data.data;}
private:
Stored_Data_Type m_data;
};
class a_class
{
public:
a_class(int init): m_data(init) {};
void set(const int data) {m_data = data;};
const int get() const {return m_data;};
private:
int m_data;
};
class b_class : public a_class
{
public:
b_class(): a_class{0} {};
};
int main()
{
//a_class b(1);
b_class b;
b.set(2);
std::cout << "b: " << b.get() << std::endl;
my_template<int> my_int;
my_int.write() = 10;
std::cout << "my_int: " << my_int.get() << std::endl;
my_template<b_class> my_b;
my_b.write().set(2);
std::cout << "my_b: " << my_b.get().get() << std::endl;
// Compile error here:
my_template<a_class> my_a(1);
std::cout << "my_a: " << my_a.get().get() << std::endl;
}
Upvotes: 3
Reputation: 12283
You are passing a_class
as a template parameter to the my_template
template class and, since inside my_template
class Stored_Data_Type m_data;
is an object of following struct
struct Stored_Data_Type {
a_class data;
int set_count = 0;
};
m_data
member is default constructed with
my_template() : m_data() {}
which tries to call a_class
's default constructor which you haven't defined.
Therefore, you just need to define default constructor for your a_class
class:
class a_class {
public:
a_class()
: m_data{0}
{}
// ...
};
Check live example
Upvotes: 4