【发布时间】:2018-07-16 08:35:45
【问题描述】:
这段代码:
#include <memory>
template <template <typename> class Ptr>
class A { Ptr<int> ints; };
using B = A<std::unique_ptr>;
产生以下错误(使用 GCC 6.3):
a.cpp:6:28: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class> class Ptr> class A’
using B = A<std::unique_ptr>;
^
a.cpp:6:28: note: expected a template of type ‘template<class> class Ptr’, got ‘template<class _Tp, class _Dp> class std::unique_ptr’
现在,我可以像这样解决这个问题:
template <typename T>
using plugged_unique_ptr = std::unique_ptr<T>;
using B = A<plugged_unique_ptr>;
但为什么我必须这样做?我的意思是,为什么编译器不愿意将std::unique_ptr 的第二个模板参数“插入”其默认值并允许将std::unique_ptr 用作A 的模板参数?
【问题讨论】:
-
注意它应该从
C++17开始编译。 -
@HolyBlackCat 你能告诉我为什么 C++17 能做到这一点吗?
-
@rubenvb This page 有一些解释。
-
@HolyBlackCat 我猜你的意思是该页面上的这一点:“要将模板模板参数A匹配到模板模板参数P,A的每个模板参数都必须匹配相应的模板P 的参数精确(C++17 前)P 必须至少与 A 一样特化(C++17 起)"
标签: c++ c++11 unique-ptr template-argument-deduction template-templates