通过模板模板参数的继承访问静态数据?

Accessing static data through inheritance of a template template parameter?

template
<
    template <typename, typename>
        class storage_t,
    typename T,
    typename is_allocated
>
class Buffer : public storage_t<T, is_allocated> { ... };

template
<
    template <typename, typename>
        class storage_t,
    typename T = storage::UnknownType,
    typename is_allocated = std::false_type
>
class Example_Buffer
: public Buffer<storage_t, T, is_allocated> {
    constexpr Example_Buffer(
        typename storage_t<T, is_allocated>::iterator it) {}
};

Example_Buffer<...> 继承自 Buffer<...>Buffer<storage_t, T, is_allocated> 继承自 storage_t<T, is_allocated>storage_t<...> 包括 typedefs 和静态 constexpr 数据。有没有办法在Example_Buffer的构造函数中通过继承访问这些typedefsstatic constexpr data? (通过继承,即不使用storage_t<T, is_allocated>?在同一个class.

中两次使用这种语法感觉有点奇怪

欢迎询问我是否需要详细说明。

成员只要在 storage_t 中 public 就可以继承和访问。您可以使用 injected class name and/or 注入的基础 class 名称来访问这些依赖成员,

这里有几个选项..

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

using namespace std;

template <typename A,typename B>
struct basic_storage
{
    using a_type = A;
    using b_type = B;
    static constexpr bool value = b_type::value;
};


template
<
    template <typename, typename>
        class storage_t,
    typename T,
    typename is_allocated
>
class Buffer : public storage_t<T, is_allocated> {

    public:
    using storage_type = storage_t<T, is_allocated>;
};

template
<
    template <typename, typename>
        class storage_t,
    typename T /*= storage::UnknownType*/,
    typename is_allocated = std::false_type
>
class Example_Buffer
: public Buffer<storage_t, T, is_allocated> {
    public:
    constexpr Example_Buffer(
        /*typename storage_t<T, is_allocated>::iterator it*/) {

            //using the members with the injected class name..
            using b_type = typename Example_Buffer::b_type;

            // or directly using injected class name..
            std::cout << typeid(typename Example_Buffer::a_type).name() << std::endl;
            std::cout << Example_Buffer::value << std::endl;

            // using storage_type defined in Buffer<...>
            using storage_type = typename Example_Buffer::storage_type;

            std::cout << typeid(typename storage_type::a_type).name() << std::endl;
            std::cout << storage_type::b_type::value << std::endl;
            std::cout << storage_type::value << std::endl;

        }
};


int main() {
    Example_Buffer<basic_storage,int,std::true_type>{};
    return 0;
}

Demo

如果你想知道为什么你不能在派生的 class 中访问 then 而没有在它们前面加上 Example_Buffer:: 基础 class 名称,正如 this post 解释的那样,这是因为它们是从属名称。