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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:09:14+00:00 2026-06-05T02:09:14+00:00

I’m trying to create a global map that will map strings to factory functions.

  • 0

I’m trying to create a global map that will map strings to factory functions. This allows me to have a builder function read a key from an ini file, and then pass that ini file with its relevant section to the correct factory function. Here is the code in the header with the global map:

typedef Primitive* (*factory_func_t)(const ::INI&, const std::string&);

const std::map<std::string, factory_func_t> factory_funcs = {
    { SphereFactory::ID,     &SphereFactory::create_sphere },
    { QuadFactory::QUAD_ID,  &QuadFactory::create_quad },
    { QuadFactory::PQUAD_ID, &QuadFactory::create_pquad }
};

And here is an example of one of those factory classes:

class SphereFactory
{
  public:
    static const std::string ID;

    static Sphere* create_sphere(const ::INI&, const std::string& section);

    SphereFactory() = delete;

  private:
    static const std::string CENTER_KEY;
    static const std::string RADIUS_KEY;
};

const std::string SphereFactory::ID = "Sphere";

const std::string SphereFactory::CENTER_KEY = "Center";
const std::string SphereFactory::RADIUS_KEY = "Radius";

All this is giving me an error when compiling:

error: could not convert `{{cg::prim::factory::SphereFactory::ID,
 cg::prim::factory::SphereFactory::create_sphere}, 
 {cg::prim::factory::QuadFactory::QUAD_ID, 
 cg::prim::factory::QuadFactory::create_quad}, 
 {cg::prim::factory::QuadFactory::PQUAD_ID, 
 cg::prim::factory::QuadFactory::create_pquad}}'
 from `<brace-enclosed initializer list>'
 to `const std::map<std::basic_string<char>, cg::Primitive* (*)(const INI&, 
 const std::basic_string<char>&)>'

All of the above code is in the cg::prim::factory namespace, in case that makes a difference. Both Quad and Sphere inherit from Primitive. I’m using g++ -O3 -Wall -Wextra -pedantic -std=c++11 to compile.

Why is this not compiling?

  • 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-05T02:09:16+00:00Added an answer on June 5, 2026 at 2:09 am

    You are trying to use covariant return types: the typedef is for Primitive * but the factories return Sphere *, etc. You might include alternative, wrapper factory functions that return the base type.

    As it is, C++ sees no relationship between the functions returning base and derived pointers. Only virtual functions are allowed to be “converted” that way, and factories can’t be virtual. (Or at least, in C++ factory objects with virtual functions are discouraged. That would be typical in Java, but here a wrapper function would be better.)

    Since initializer lists are still in their infancy, one way to debug (and work around potential compiler bugs that may still exist) is to manually cast things wherever possible:

    const std::map<std::string, factory_func_t> factory_funcs = {
        { SphereFactory::ID,     (factory_func_t) &SphereFactory::create_sphere },
        { QuadFactory::QUAD_ID,  (factory_func_t) &QuadFactory::create_quad },
        { QuadFactory::PQUAD_ID, (factory_func_t) &QuadFactory::create_pquad }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

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.