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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:01:55+00:00 2026-05-22T02:01:55+00:00

I read the Wikipedia article about the curiously recurring template pattern in C++ for

  • 0

I read the Wikipedia article about the curiously recurring template pattern in C++ for doing static (read: compile-time) polymorphism. I wanted to generalize it so that I could change the return types of the functions based on the derived type. (This seems like it should be possible since the base type knows the derived type from the template parameter). Unfortunately, the following code won’t compile using MSVC 2010 (I don’t have easy access to gcc right now so I haven’t tried it yet). Anyone know why?

template <typename derived_t>
class base {
public:
    typedef typename derived_t::value_type value_type;
    value_type foo() {
        return static_cast<derived_t*>(this)->foo();
    }
};

template <typename T>
class derived : public base<derived<T> > {
public:
    typedef T value_type;
    value_type foo() {
        return T(); //return some T object (assumes T is default constructable)
    }
};

int main() {
    derived<int> a;
}

BTW, I have a work-around using extra template parameters, but I don’t like it—it will get very verbose when passing many types up the inheritance chain.

template <typename derived_t, typename value_type>
class base { ... };

template <typename T>
class derived : public base<derived<T>,T> { ... };

EDIT:

The error message that MSVC 2010 gives in this situation is error C2039: 'value_type' : is not a member of 'derived<T>'

g++ 4.1.2 (via codepad.org) says error: no type named 'value_type' in 'class derived<int>'

  • 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-22T02:01:56+00:00Added an answer on May 22, 2026 at 2:01 am

    derived is incomplete when you use it as a template argument to base in its base classes list.

    A common workaround is to use a traits class template. Here’s your example, traitsified. This shows how you can use both types and functions from the derived class through the traits.

    // Declare a base_traits traits class template:
    template <typename derived_t> 
    struct base_traits;
    
    // Define the base class that uses the traits:
    template <typename derived_t> 
    struct base { 
        typedef typename base_traits<derived_t>::value_type value_type;
        value_type base_foo() {
            return base_traits<derived_t>::call_foo(static_cast<derived_t*>(this));
        }
    };
    
    // Define the derived class; it can use the traits too:
    template <typename T>
    struct derived : base<derived<T> > { 
        typedef typename base_traits<derived>::value_type value_type;
    
        value_type derived_foo() { 
            return value_type(); 
        }
    };
    
    // Declare and define a base_traits specialization for derived:
    template <typename T> 
    struct base_traits<derived<T> > {
        typedef T value_type;
    
        static value_type call_foo(derived<T>* x) { 
            return x->derived_foo(); 
        }
    };
    

    You just need to specialize base_traits for any types that you use for the template argument derived_t of base and make sure that each specialization provides all of the members that base requires.

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

Sidebar

Related Questions

I'm trying to learn about catamorphisms and I've read the Wikipedia article and the
I read the Wikipedia article about md5 hashes but I still can't understand how
I just recently heard of duck typing and I read the Wikipedia article about
I read the following paragraph on Wikipedia article about the CLR: Alternatively, the CIL
I read the german article about Make on Wikipedia and found the following 2
I read the Wikipedia article on scenario testing, but I am sad to say
I just read the Wikipedia article on mock objects , but I'm still not
I've read the article on Wikipedia on the Duff's device , and I don't
I read the article on wikipedia but could not understand what exactly are NP
some time ago I read an article that it is possible to use the

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.