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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:02:13+00:00 2026-06-16T10:02:13+00:00

Consider the following example: #include <iostream> #include <iostream> #include <type_traits> template<typename Type, template<typename> class

  • 0

Consider the following example:

#include <iostream>
#include <iostream>
#include <type_traits>

template<typename Type, template<typename> class Crtp>
class Base
{
    public:
        typedef int value;

        // f1: OK
        // Expected result: casts 4.2 to Base<Type, Crtp>::value
        value f1() {return 4.2;}

        // f2: NOT OK
        // Expected result: casts 4.2 to Crtp<Type>::value
        // But f2 does not compile: no type named 'value' 
        // in 'class Derived<double>'
        typename Crtp<Type>::value f2() {return 4.2;} 
};

template<typename Type>
class Derived : public Base<Type, Derived>
{
    public:
        typedef Type value;
};

int main()
{
    Derived<double> a;
    std::cout<<a.f1()<<std::endl;
    std::cout<<a.f2()<<std::endl;
    return 0;
}

How to solve this problem (Derived typedef unknown from the Base class) ?

EDIT: I’ve found a very simple trick. Can someone explain to me why the following is working and the previous version does not work? Is this trick ok with standard C++11 or it works because of the way the compiler works (here g++ 4.7.1) ?

#include <iostream>
#include <iostream>
#include <type_traits>

template<typename Type, template<typename> class Crtp>
class Base
{
    public:
        typedef int value;
        value f1() {return 4.2;}
        template<typename T = Crtp<Type>> typename T::value f2() {return 4.2;}
};

template<typename Type>
class Derived : public Base<Type, Derived>
{
    public:
        typedef Type value;
};

int main()
{
    Derived<double> a;
    std::cout<<a.f1()<<std::endl;
    std::cout<<a.f2()<<std::endl;
    return 0;
}
  • 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-16T10:02:14+00:00Added an answer on June 16, 2026 at 10:02 am

    You have to use a wrapper class (here it’s value_getter) which is declared before you define Base. You can then specialize it right before you defined Derived:

    template<typename T>
    struct value_getter;
    
    template<typename Type, template<typename> class Crtp>
    class Base
    {
        public:
            typedef int value;
    
            value f1() {return 4.2;}
    
            // in 'class Derived<double>'
            typename value_getter<Crtp<Type> >::value f2() {return 4.2;} 
    };
    
    template<typename Type>
    class Derived;
    
    template<typename Type>
    struct value_getter<Derived<Type> > {
        typedef Type value;
    };
    
    template<typename Type>
    class Derived : public Base<Type, Derived>, public value_getter<Derived<Type> >
    {
        public:
    };
    

    It’s not exactly pretty, but at least it works.

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

Sidebar

Related Questions

Consider following example. #include <iostream> #include <boost/optional.hpp> template < typename A > int boo(
Consider the following minimal example. #include <vector> class Data { std::vector<int>& v; public: Data(std::vector<int>&
Consider the following minimal example: #include <iostream> using namespace std; class myostream : public
Consider the following use of template template parameters... #include <iostream> template <typename X> class
Consider the following sample code: #include <iostream> using namespace std; class base { public:
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
Consider the following example: #include <iostream> #include <numeric> #include <array> #include <type_traits> // Array:
Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e,
Consider the following example: public class BaseClass { public string StringInBaseClass {get;set;} public int
Consider following example code: #include <iostream> #include <inttypes.h> using namespace std; int f(uint32_t i)

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.