c++ - 部分默认使用内部类型初始化模板模板参数

标签 c++ templates c++11 template-templates

template <template <typename> class container_type>
class MyClass
{
    class Internal{
    };
};

我想以如下方式使用此类(或它看起来正确的方式)...

MyClass(std::list);

...因此在 MyClass 中 container_type 声明/typedef 为:

std::list<Internal*>

这样的事情有可能吗?

最佳答案

您可能需要如下内容:

#include <list>
#include <memory>

template <template <typename, typename> 
          class Container = std::list>
class MyClass 
{
    class Internal
    { };

    Container<Internal*, std::allocator<Internal*>> my_list;
};

int main()
{
    MyClass<> m;
}

Here's一个你可以玩的可编译的例子。请注意,此处需要额外的 typename 和分配器的定义。

关于c++ - 部分默认使用内部类型初始化模板模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18691785/

相关文章:

c++ - vector 未在此范围内声明

amazon-web-services - ec2 的 AWS Cloudformation 默认 key 对

C++ 类模板作为属性类型

c++ - 在模板参数列表中避免自动

c++ - Lambda 和 std::function

c++ - 获取有关 Windows 将在 C++ 中 sleep /唤醒的通知

c++ - C++ 中使用指针传递 Char

c++ - Windows EVENTLOGRECORD 结构中的可变长度字段如何工作?

c++ - 为什么结构内部的对象初始化不同?

c++ - SFINAE 检查表达式是否编译并返回 std::true_type