Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9268909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:58:37+00:00 2026-06-18T14:58:37+00:00

I have a C++ class template. It takes a template parameter class T and

  • 0

I have a C++ class template. It takes a template parameter class T and stores an object of type T as a private member accessible through a method called data(). The code below demonstrates it. I’d like my class template to offer a convenient constructor which takes into account the type T. For example, a constructor which takes some of T’s fields as parameters and initializes the encapsulated T object with their values.

One way to do that is having the user derive from the template instantiation and add their own constructors there, but I’d prefer, naturally, to have a way to supply ctors without making the user write a derived class.

template <class T>
class Templ
{
public:
     T& data();
     const T& data() const;
private:
     T obj;
};

Now if a user wants a convenienve constructor, they’d have to derive Templ:

class MyClass : public Templ<MyData>
{
public:
     MyClass (int size, MyClass* parent, float temperature, std::string name);
};

I read some C++11 stuff and had ideas about having a constructor template like the STL has std::list::emplace() set of methods, but I’m not sure what’s the common best-practice solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:58:38+00:00Added an answer on June 18, 2026 at 2:58 pm

    Yeah, C++11 variadic perfect forwarding is what you want:

    template <class T>
    class Templ
    {
    public:
         template<typename... Args>
         explicit Templ(Args&&... args) : obj(std::forward<Args>(args)...) {}
    
         T& data();
         const T& data() const;
    private:
         T obj;
    };
    

    This forwards any argument of the Templ constructor to the T constructor.

    But I’m wondering, why even have this class at all? What are you trying to do?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have function which takes in an parameter of a class called Triple, and
I have an ID class template that takes a parameter T . If T
I have the following class: template <typename T> class matrix { private: int _n;
I have a template class, called OneCell , here the definition: template <class T>
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I have a template class, called Cell , here the definition: template <class T>
I have a matrix class that takes the row and column reference type that
Say I have a macro, FOO(name), and some template class Bar<> that takes one
I have a template class that I have made called hash . My template
I have a class, called DynamicTexture that takes in the width and height of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.