Jump to content
  • Advertisement
hkBattousai

C++ Initializing a template data member when the template type has no default constructor

Recommended Posts

I have a template class which is supposed to keep a data member of the template type. When this template type is a class which has no default constructor, I get the obvious compiler error message about the class to be initialized not having a default constructor. How do I initialize this template data member? Is there a generic syntax for this which will work for all possible cases?

Example code:

#include <string>

class ClassWithNoDefaultConstructor1
{
	public:
		ClassWithNoDefaultConstructor1() = delete;
		ClassWithNoDefaultConstructor1(const std::string & Str)
			: String(Str)
		{
		}
	private:
		std::string String;
};

class ClassWithNoDefaultConstructor2
{
	public:
		ClassWithNoDefaultConstructor2() = delete;
		ClassWithNoDefaultConstructor2(const int Int, const float Flt)
			: Integer(Int), Float(Flt)
		{
		}
	private:
		int Integer;
		float Float;
};

template <class T>
class ClassWithATemplateDataMemeber
{
	public:
		ClassWithATemplateDataMemeber()
			: Variable()	// Error here. How to initialize this "Variable"?
		{
		}
	private:
		T Variable;
};

int wmain(int argc, wchar_t * argv[])
{
	ClassWithATemplateDataMemeber<ClassWithNoDefaultConstructor1> MyClass1;
	ClassWithATemplateDataMemeber<ClassWithNoDefaultConstructor2> MyClass2;
	return 0;
}

 

Share this post


Link to post
Share on other sites
Advertisement

If it's really the case that you need to store a value that must be parameterized, then it's necessary to have a constructor in your template that will accept the parameters.  You need a "perfect forwarding of  parameter packs" template template constructor.  That's how templates like std::function and std::make_unique work.

If it's absolutely imperative that you be able to construct an object that depends on parameters in a place where you can not give it parameters, your design is wrong and no amount of code magic can fix it.

Share this post


Link to post
Share on other sites

After studying the parameter packs topic, I was able to find the correct syntax about how to forward parameters packs. I hope this example code helps other people who come across to the same problem in the future.

 

#include <iostream>
#include <string>
#include <utility>

class ClassWithNoDefaultConstructor1
{
	public:
		ClassWithNoDefaultConstructor1() = delete;
		ClassWithNoDefaultConstructor1(const std::string & Str)
			: String(Str)
		{
		}
		void Print()
		{
			std::cout << "ClassWithNoDefaultConstructor1: " << String << std::endl;
		}
	private:
		std::string String;
};

class ClassWithNoDefaultConstructor2
{
	public:
		ClassWithNoDefaultConstructor2() = delete;
		ClassWithNoDefaultConstructor2(const int Int, const float Flt)
			: Integer(Int), Float(Flt)
		{
		}
		void Print()
		{
			std::cout << "ClassWithNoDefaultConstructor2: " << Integer << ", " << Float << std::endl;
		}
	private:
		int Integer;
		float Float;
};

template <class T>
class ClassWithATemplateDataMemeber
{
	public:
		template <class ... Arg>
		ClassWithATemplateDataMemeber(Arg && ... Arguments)
			: Variable(std::forward<Arg>(Arguments) ...)
		{
		}
	//private:
		T Variable;
};

int wmain(int argc, wchar_t * argv[])
{
	ClassWithATemplateDataMemeber<ClassWithNoDefaultConstructor1> MyClass1(std::string("String"));
	ClassWithATemplateDataMemeber<ClassWithNoDefaultConstructor2> MyClass2(5, 3.14f);
	
	MyClass1.Variable.Print();
	MyClass2.Variable.Print();
	
	system("puase");
	return 0;
}

 

Output:

Quote

ClassWithNoDefaultConstructor1: String
ClassWithNoDefaultConstructor2: 5, 3.14

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!