1

I'm not sure what I should search for, so I'm sorry if this question have already been asked.

I have the following code

namespace ns {
    template<typename crtp> struct  base_type {
        template<typename ...Args> base_type(Args &&...args);
    };
}

struct derived_type: ns::base_type<derived_type> {
    derived_type(int a, float b): base_type{a, b} {}
};

template<typename template_arg>
struct derived_template_type: ns::base_type<derived_template_type<template_arg>> {
    derived_template_type(int a, float b): base_type{a, b} {}
};

derived_type compiles fine. derived_template_type fails to compile with following error (clang, c++17):

error: member initializer 'base_type' does not name a non-static data member or base class
    derived_template_type(int a, float b): base_type{a, b} {}
                                           ^~~~~~~~~~~~~~~

To make it work, I have to replace this usage of base_type with it's fully-qualified name ns::base_type<derived_template_type<template_arg>> - so basically repeat what is already there on the base classes' list.

First question is: why is it so? The compiler should be aware that this is the base class, know where it's located and what are its template parameters - like in case of derived_type - but apparently, it's not. Which language rule causes it to behave this way?

Second question is (obviously): can I do something to avoid this repeating?

notsurewhattodo
  • 446
  • 4
  • 11
  • 1
    [call base class constructor without naming its class](https://stackoverflow.com/questions/39413091/call-base-class-constructor-without-naming-its-class) is the closest candidate duplicate I found. – cpplearner May 19 '19 at 19:18
  • 1
    I wonder why it accept ```derived_type(int a, float b): base_type{a, b} {}``` in case of ```struct derived_type: ns::base_type```. To me that is more of a strange behavior! :) – AKL May 19 '19 at 19:26
  • @cpplearner thanks, this indeed looks like a solution for me - using "derived_template_type::base_type" seems to solve the problem (it's apparently called "injected class name"). So it seems to answer my second question. I'd still like to know what's going on there (my "first question"). – notsurewhattodo May 19 '19 at 19:30
  • 1
    The explanation provided in the answer for [Compiler Error When Calling Base Constructor when both Base and Derived are Templated With Derived Type Parameter](https://stackoverflow.com/questions/43712270/compiler-error-when-calling-base-constructor-when-both-base-and-derived-are-temp/43712304#43712304) seems good enough to me. – cpplearner May 19 '19 at 19:34
  • @cpplearner you're right, it explains everything. Thanks again. Apparently I was too confused to properly formulate the search query. I guess, my question can be closed as a duplicate. – notsurewhattodo May 19 '19 at 19:38

0 Answers0