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

  • SEARCH
  • Home
  • 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 8335519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:42:04+00:00 2026-06-09T03:42:04+00:00

Is it possible to define a class which is not a class template and

  • 0

Is it possible to define a class which is not a class template and that can store (say upon construction) a reference to any particular type and retrieve it later via a getter method?

struct Wrapper {
  template<typename T> Wrapper(const T& t): t_(t);

  // How to store the reference??
};

Boost::variant is of course not a solution since its implemented as a class template. And I have no RTTI. (Its in HPC environment, performance is all!)

The getter() should be able to remember the type. So that auto can be used later.

  • 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-09T03:42:05+00:00Added an answer on June 9, 2026 at 3:42 am

    The getter() should be able to remember the type.

    It can’t. You erased the type. That’s what happens when you put a value of arbitrary type inside an object of a consistent type. It’s called "type erasure": the original type is removed from the containing class.

    One of the consequences of this is that the type cannot magically be recovered. You can’t have a class that is initialized with a type on a per-instance basis, and then have one of its member functions return a different type based on how that particular instance was initialized.

    C++ is a statically typed language. The return type of every function must be defined at compile time. It cannot change because of what you put into a class at runtime.

    It can’t remember the type.

    I can’t use RTTI.

    Then what you want is a void*. RTTI is the only way to do recover the original value’s type in a type-safe way. If you don’t want type-safety, then you want a void*. All you would be doing is writing a wrapper around static_cast<T*>.

    There’s really no point in writing a wrapper around void* and static_cast. If that’s what you want, and you’re willing to accept the perils of using them (ie: breaking the type-system), then just use it. The reason boost::any exists is to provide a type-safe void*, so that at least you know when you’re casting it wrongly.

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

Sidebar

Related Questions

I have noticed that it is possible to define a custom class and then
Can I define a class which derives from DynamicObject and supports an interface (ICanDoManyThings)
Possible Duplicate: How do I define a template function within a template class outside
Let's say I have a class that has a member which is an array.
Is it possible to write generic class with one constructor which explicitly defines type
Possible Duplicate: Define a method that has many (or infinite) arguments I have the
Possible Duplicate: Is it possible to define enumalpha? Is there any equivalent of Java
I want to define a generic class in c++ that allow to execute my
I would like to define inside a class a constant which value is the
I have a class which I cant change (SuperClass) that has an interface for

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.