c++ - 使期望单个模板参数的模板模板参数的类接受可变模板参数的模板模板参数?

标签 c++ templates c++14 variadic-templates template-templates

我正在尝试编写类似于 std::bind 的内容,但对于模板类型而不是对象。实际上,我想将一个模板参数绑定(bind)到一个模板类型。 (以一种类似的方式,在 std::bind 中,我们将一个(或多个)真实参数绑定(bind)到一个函数式对象。)

下面的 C++ 代码最能说明我想要什么:

#include <tuple>
#include <type_traits>

using namespace std;

/* This struct should be in some header-only utility library */
template <template <typename...> typename Template, typename T>
struct template_bind {
    template <typename... Args>
    using template_type = Template<T, Args...>;
};


/* This class should be in a separate header file */
// If we change the following line to add '...' then it works
// template <template <typename...> typename Template>
template <template <typename> typename Template>
class my_complicated_class {
public:
    /* stuff */
private:
    Template<float> data_;
};

/* This probably would be in a third file that includes both headers */
int main()
{
    using t1 = template_bind<std::tuple, int>;
    using t2 = template_bind<t1::template_type, double>;
    my_complicated_class<t2::template_type> stuff_with_tuple; // Compile error

    using p1 = template_bind<std::pair, int>;
    my_complicated_class<p1::template_type> stuff_with_pair; // Compile error
}

有趣的是,代码可以在 GCC 和 MSVC 上使用 C++17 进行编译,但不能在 Clang(使用任何 C++ 标准)或 GCC/MSVC 上使用 C++14 进行编译。错误(在那些不会编译它的编译器上)是 my_complicated_class需要一个接受单个模板参数的模板模板参数,但是template_type是一个可变参数模板。

改变 my_complicated_class接受可变模板模板参数可以解决所有编译器上的问题。但是,改成my_complicated_class会感觉怪怪的接受可变模板模板参数,因为 my_complicated_class可能不应该对所使用的模板模板参数一无所知。 (否则,可以提出所有模板模板参数都应写成可变参数模板的论点,例如 template <template <typename...> typename Template> ,但这似乎不是模板模板参数通常的写法。)

哪些编译器不符合标准,我怎样才能使代码在 C++14 编译器上编译?

最佳答案

Clang 不支持 C++17 模板模板参数/参数匹配。

相关段落:

A template-argument matches a template template-parameter (call it P) when each of the template parameters in the template-parameter-list of the template-argument's corresponding class template or alias template (call it A) matches the corresponding template parameter in the template-parameter-list of P. [...]

A template-argument matches a template template-parameter P when P is at least as specialized as the template-argument A. [...]

这是使您的代码在 C++17 中格式良好的最后一句话。当 P 为 template<class> class A 是 template<class...> class , P 比 A 更专业。

关于c++ - 使期望单个模板参数的模板模板参数的类接受可变模板参数的模板模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57016037/

相关文章:

c++ - 修改参数的函数的完美返回

c++ - 对头文件稍作修改是否需要重新编译第三方库

c++ - 仅当模板参数中存在 typedef 时才创建 typedef

class - 模板参数 : enum, 类或枚举类

带有 Clang 的 QtCreator 中的 C++14 支持

c++ - 强制执行不同的 C++ 模板参数

c++ - 具有非空模板参数列表的模板特化

c++ - C++ 函数模板中的参数可以转换吗?

c++ - 为什么非模板类要在头文件和源文件中分开?

c++ - 带有自定义删除器的工厂函数返回 unique_ptr