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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:39:21+00:00 2026-05-20T21:39:21+00:00

This is a strange question because I already know the ‘coding’ answer. I just

  • 0

This is a strange question because I already know the ‘coding’ answer. I just want to get a better understanding of why it is so. There are guru’s here who have a knack of explaining these things better than the C++ standard 🙂

Below we have a means to define an abstract factory template that allocates objects based on a string as a key (it is a contrived example):-

#include <iostream>
#include <map>
#include <string>

using namespace std;

template <typename T, typename TProduct>
TProduct *MyFactoryConstructHelper(const T *t)
{
  if (!t) return new T;
  return new T(*static_cast<const T*>(t));
}

template <typename TProduct>
class AbstractFactory
{
public:
  typedef TProduct *(*MyFactoryConstructor)(const void *);
  typedef map<string, MyFactoryConstructor> MyFactoryConstructorMap;

  static TProduct *Create(const string &iName)
  {
    MyFactoryConstructor ctr = mTypes[iName];
    TProduct *result = NULL;
    if(ctr) result = ctr(NULL);
    return result;
  }

  template <typename T>
  static bool Register(const string &iName) {
    typedef TProduct*(*ConstructPtr)(const T*);
    ConstructPtr cPtr = MyFactoryConstructHelper<T, TProduct>;
    string name = iName;
    mTypes.insert(pair<string,MyFactoryConstructor>(name, reinterpret_cast<MyFactoryConstructor>(cPtr)));
    return(true);
  }

protected:
  AbstractFactory() {}
  static MyFactoryConstructorMap mTypes;
};

template <typename TProduct>
map<string, /*typename*/ AbstractFactory<TProduct>::MyFactoryConstructor> AbstractFactory<TProduct>::mTypes;

Here is an example of how we use it: –

class MyProduct
{
public:
  virtual ~MyProduct() {}

  virtual void Iam() = 0;
};

class MyProductFactory : public AbstractFactory<MyProduct> 
{
public:
};

class ProductA : public MyProduct
{
public:
  void Iam() { cout << "ProductA" << endl; }
};

class ProductB : public MyProduct
{
public:
  void Iam() { cout << "ProductB" << endl; }
};

int _tmain(int argc, _TCHAR* argv[])
{
  MyProduct *prd;
  MyProductFactory::Register<ProductA>("A");
  MyProductFactory::Register<ProductB>("B");

  prd = MyProductFactory::Create("A");  
  prd->Iam();
  delete prd; 
  prd = MyProductFactory::Create("B");  
  prd->Iam();
  delete prd;

  return 0;
}

It will not compile as is, complaining that the map does not have a valid template type argument for the data type. But if you remove the comments around the ‘typename’ keyword in the static member definition, everything compiles and works fine… why?

and also, can I make this any better? 🙂

  • 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-20T21:39:22+00:00Added an answer on May 20, 2026 at 9:39 pm

    The standard tries to allow an implementation to parse and
    detect as many errors in a template as possible when it reads
    the template definition, before any instantiations. C++ is not
    context independent, however, and it’s very difficult, if not
    impossible, to correctly parse statements if you don’t know
    which symbols are types and which are templates. If the symbol
    is dependent (depends on the template parameters in some way),
    you have to tell the compiler when it is a type or a template;
    otherwise, the compiler must assume that it is something else.
    In this case, you’re telling the compiler that
    AbstractFactory::MyFactoryConstructor names a type,
    and not something else.

    If, when the template is instantiated, and the compiler can see
    to what the symbol is really bound, it turns out that you lied
    (e.g. AbstractFactory::MyFactoryConstructor is in fact
    an int), then the compiler will get mad at you.

    Note too that the fact that the AbstractFactory was defined
    before the definition requiring the typedef doesn’t change
    anything. There could always be an explicit specialization for
    the type you’re instantiating AbstractFactory on.

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

Sidebar

Related Questions

I know this question might sound strange because GLib is a portability library, but
This might seam like a strange question but.... public string MyProperty { get {
I know that even a question Erlang or Java seems quite strange, because they
Well, that might be a strange question, and maybe just because I'm not familiar
I am hesitated to ask this question because it looks weird. But anyway. Just
maybe I have a strange question today. I already solved this at the moment
It's a strange and silly question I know, but I'm curious because I don't
I know this may seem like a strange question, but say I have two
A coworker's typo when calling a subref raised this strange syntax question. If I
this is really strange, yesterday I asked the question Yesterdays question how to implement

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.