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 6759059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:53:11+00:00 2026-05-26T13:53:11+00:00

I have an abstract class (I know that it will not compile this way,

  • 0

I have an abstract class (I know that it will not compile this way, but it’s for comprehension of what I want to do) :

class AbstractComputation {
    public:
        template <class T> virtual void setData(std::string id, T data);
        template <class T> virtual T getData(std::string id);
};

class Computation : public AbstractComputation {
    public:
        template <class T> void setData(std::string id, T data);
        template <class T> T getData(std::string id, T data);
};

So when I call setData<double>("foodouble", data) I want the double identified by foodouble (internal mechanism which is not the main concern here) to be set to the double data.

So how to do that?

I think that there may be a mean by typing something like virtual void setData<double>(std::string id, double data) but I don’t know how to do it.

  • 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-05-26T13:53:12+00:00Added an answer on May 26, 2026 at 1:53 pm

    The problem is that you cannot mix static time polymorphism (templates) with runtime polymorphism easily. The reason for the language disallowing the particular construct in your example is that there are potentially infinite different types that could be instantiating your template member function, and that in turn means that the compiler would have to generate code to dynamically dispatch those many types, which is infeasible.

    There are different things that can be done here to get around the limitation, basically either take away the static or the dynamic polymorphism. Removing dynamic polymorphism from the equation could be done by providing a type that is not derived from, to store the <key,value> mappings, and then offering the template that resolves that only at the base level:

    class AbstractComputation {
    public:
       template <typename T>
       void setData( std::string const & id, T value ) {
          m_store.setData( id, value );
       }
       template <typename T>
       T getData( std::string const & id ) const {
          return m_store.getData<T>( id );
       }
    protected:
       ValueStore m_store;
    };
    

    Now deriving classes can access the ValueStore from the base and there is no need for polymorphism. (This can also be done by implementing the functionality directly in AbstractComputation but it probably makes sense to separate concerns)

    The other option is to maintain runtime polymorphism, but remove static polymorphism. This can be done by performing type erasure on the base class and then dispatching to the appropriate (non-templated) function that takes the type-erased arguments. The simplest version of this is just using boost::any:

    class AbstractComputation {
    public:
       template <typename T>
       void setData( std::string const & id, T value ) {
          setDataImpl( id, boost::any( value ) );
       }
       template <typename T>
       T getData( std::string const & id ) const {
          boost::any res = getDataImpl( id );
          return boost::any_cast<T>( res );
       }
    protected:
       virtual void setDataImpl( std::string const & id, boost::any const & value ) = 0;
       virtual boost::any getDataImpl( std::string const & id ) const = 0;
    };
    

    How type erasure is implemented under the hood is interesting, but out of the scope here, the important part is that a boost::any is a concrete (non-templated) type that can store any type internally by using type erasure on the arguments, and at the same time allows for type-safe retrieval of the data.

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

Sidebar

Related Questions

I have an abstract class that implements IDisposable, like so: public abstract class ConnectionAccessor
I have an abstract class BaseItem declared like this: public abstract class BaseItem {
I have an abstract class A that define abstract methods. This means that, for
I simply want a class that does this: class cleanup : boost::noncopyable { public:
I have an abstract class Camera. There are some derived classes that will handle
Let say I have abstract class called: Tenant and Customer. The tenant in this
Suppose we have abstract class A (all examples in C#) public abstract class A
I have an abstract base class with a TcpClient field: public abstract class ControllerBase
I have an abstract class: type TInterfaceMethod = class abstract public destructor Destroy; virtual;
I have an abstract class, Foo, that has a non-abstract method called Bar. I

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.