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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:42:17+00:00 2026-06-14T17:42:17+00:00

I wrote a C++ template class, but I do not have the type to

  • 0

I wrote a C++ template class, but I do not have the type to instantiate the class. Types are stored in a string format. So I have to do something like this:

if ( propType == "char") {
   Property<char> pChar = ...
} else if ( propType == "int") {
   Property<int> pChar = ...
} if ( propType == "double") {
   Property<double> pChar = ...
} 

I am not liking this if-else- loop, is there any way to avoid this, or any other solution to such problem?

  • 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-14T17:42:19+00:00Added an answer on June 14, 2026 at 5:42 pm

    Many ways, but it’s impossible to choose a good one without knowing how you use it in your particular case.

    As a demonstration:

    • Let us suppose that all Property<T> classes inherit from PropertyBase
    • Let us suppose that you initialize them by parsing the type from a string

    Here is some code then:

    using PropertyPtr = std::unique_ptr<PropertyBase>;
    using Parser = std::function<PropertyPtr(std::string const&, std::string const&)>;
    
    template <typename T>
    PropertyPtr parse(std::string const& type, std::string const& value) {
        T v = boost::lexical_cast<T>(value);
        return PropertyPtr(new Property<T>(std::move(v)));
    }
    
    std::map<std::string, Parser> const parsers = {
        std::make_pair("char", parse<char>),
        std::make_pair("int", parse<int>),
        std::make_pair("double", parse<double>)
    };
    
    void dummy(std::string const& type, std::string const& value) {
        auto const it = parsers.find(type);
        assert(it == parsers.end() && "No parser");
    
        auto const& parser = it->second;
        PropertyPtr property = parser(type, value);
    
        // do something with property
    }
    

    Hope this helps.

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

Sidebar

Related Questions

We have some code that looks like this: class Serializer { public: template<class Type>
I wrote this little code std::map<int,template<class T>> map_; map_.insert(make_pair<int,message>(myMsg.id,myMsg)); but the compiler doesn't seem
I have my templated container class that looks like this: template< class KeyType, class
In C# I can write something like this: class AnyThing<T> { static public T
I have a matrix class like below: template <size_t M, size_t N, typename T>
I have a template class which looks like the following: template <template <class TypeT>
I have a class template Foo<T> . I'd like to implement a non-member function
I wrote the following program #include <iostream> template<typename C, typename Res, typename... Args> class
I am trying to write a template class in C++ and getting this strange
I wrote a sparse vector class (see #1 , #2 .) I would like

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.