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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:53:57+00:00 2026-06-05T14:53:57+00:00

I want to make a class that will have a single get template method

  • 0

I want to make a class that will have a single get template method which will receive an std::string to find in a std::map the right variable and return it.

The std::map should store any type of variable, so I used boost::any, so far the std::map looks like that:

std::map<std::string, boost::any> variables_;

for the get function, I tried something like that:

template <typename T>
T get(std::string& parameter)
{
    return variables_[parameter];
}

But no lucky, my question is, is that even possible to do? If so, how?

The basic idea is that I dont want to make an specific method to every specific variable in my class, so other classes dont need to know about every get method of it.

Thanks!

ps: For anyone asking why I want this, here is a resume of it, I have a lot of algorithms, that will run in a certain order, and it will use that for the last one already runned algorithm. So, what I want is to make an xml file, that will tell what algorithms will run, in which order and what data it will use from another algorithm.

So, for example, algorithm A have an variable named “threshold”, algorithm B need that information, so, normally it will have to ask it from the A using something like A.getThreshold, but as far as I know, I can’t call a object function with it name in an string (from the xml file), so my solution would be have only an get function which i pass the variable name I want and that function will return it to me.

  • 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-05T14:53:58+00:00Added an answer on June 5, 2026 at 2:53 pm

    An alternative solution would be to “wrap” the boost::any object into another object which can be automatically converted to anything you want. I don’t think it’s a good practice but it’s the best fit according to your question.

    class AnyWrapper {
        boost::any value;
    public:
        AnyWrapper(const boost::any& val) : value(val) {}
        template<typename T> operator T() {
            return boost::any_cast<T>(value);
        }
    }
    

    And your getter would be something like :

    AnyWrapper get(std::string& parameter)
    {
        return variables_[parameter];   // variables is a std::map<std::string, boost::any> 
    }
    

    And then you should be able to retrieve your elements like that :

    int integerValue = myContainer.get("age");
    std::string stringValue = myContainer.get("name");
    

    But again, this is not a clean solution. There is a reason why the boost authors chose to make the any_cast explicit 🙂

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

Sidebar

Related Questions

So I have this method that get executed repeatedly public static boolean isReady(String dirPath,
I have style sheet with a class name changebackgroundcolor i want make change in
I have a class Maze (extends JPanel). I want to make a new button,
I have a class, suppose it's called EntityModel , and I want to make
I want to use a category to make a method on the original class
I have a class set up that ideally will read the methods of any
I want to make a database restore function that will parse a dump made
I have a class that need to make some magic with every operator, like
I want to make an abstract class in c++ with a single, but with
I don't want to make a new instance of Form1 in the class, and

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.