gpt4 book ai didi

c++ - 使用模板模板参数时是否需要显式列出默认参数?

转载 作者:IT老高 更新时间:2023-10-28 21:48:20 35 4
gpt4 key购买 nike

我想问一下下面的代码示例是否应该编译:

#include <iostream>
#include <vector>
#include <typeinfo>

using namespace std;

template <template <class...> class C>
struct convert_container
{
using type = C<double>;

// Visual Studio requires this to be:
// using type = C<double, std::allocator<doble>>
};

int main()
{
std::cout << typeid(convert_container<std::vector>::type).name();
}

代码在 GCC 4.8.1 和 Clang 3.4 上编译得很好,但在 Visual Studio 2013 上编译得不好。我得到的错误:

error C2976: 'std::vector' : too few template arguments
c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(650) : see declaration of 'std::vector'
c:\users\michał\documents\visual studio 2013\projects\transform\transform.cpp(14) : see reference to class template instantiation 'convert_container<std::vector>' being compiled

标准对此有何评论?使用模板模板参数 C 时是否需要显式声明所有参数(包括默认参数)还是这只是 VC++ 中的错误?

上下文:构造函数对我上一个问题的回答引发的问题:https://stackoverflow.com/a/23874768/2617356

在搜索文件时,我发现了这个问题:Default values in templates with template arguments ( C++ )这基本上是同一个问题,问题作者指出模板模板参数的默认参数“必须”明确说明。但是,提问者接受了在我的情况下不太适用的解决方案。问题不是关于什么是符合标准的行为,所以我相信这不是重复的。

最佳答案

考虑类似

template <typename = void, int = 0> struct A { };
template <template <typename ...> class T> struct B : T<> { };
template class B<A>;

标准明确涵盖了这一点(14.3.3p3,如果您有兴趣,我不会引用它,因为 GCC 和 clang 都已经实现了该规则),其中使用 A作为 B 的模板参数由于非类型模板参数而被禁止。如果模板模板参数的实例化可以使用模板模板参数的默认模板参数,则该规则没有任何意义,因此 MSVC 和 Intel 的行为比 GCC 和 clang 的行为更加一致。

当然,“如果这是有效的,标准就会有不一致”的推理实际上并不意味着它是无效的,只是它不应该是有效的。要实际检查标准的内容:

14.1 Template parameters [temp.param]

10 The set of default template-arguments available for use with a template declaration or definition is obtained by merging the default arguments from the definition (if in scope) and all declarations in scope in the same way default function arguments are (8.3.6).

8.3.6 Default arguments [dcl.fct.default]

4 Declarations in different scopes have completely distinct sets of default arguments. That is, declarations in inner scopes do not acquire default arguments from declarations in outer scopes, and vice versa.

虽然不是专门用于解决这种使用默认模板参数的问题,但我认为它确实可以做到这一点。 Nikos Athanasiou 已经包含了标准中说明 C 的任何默认模板参数的部分。习惯了:

14.1 Template parameters [temp.param]

14 A template-parameter of a template template-parameter is permitted to have a default template-argument. When such default arguments are specified, they apply to the template template-parameter in the scope of the template template-parameter.

自从 C的默认模板参数被使用,std::vector不是,MSVC 和英特尔在这里似乎是正确的。

并且想出一个例子来清楚地表明GCC和clang在这里不能被认为是符合的:

template <typename = char, typename = short>
struct A { };

template <template <typename = void, typename ...> class T>
struct B {
using type = T<>;
};

GCC 和 clang 都处理 B<A>::type作为 A<void, short> , 从 T 中获取一个默认模板参数,另一个来自 A ,即使标准不允许在不同范围的声明中合并默认参数(以及因此默认模板参数)。


为您避免键入分配器参数的需要,一种解决方法可能是使用模板别名:

template <template <class...> class C>
struct convert_container
{
using type = C<double>;
};

template <typename T>
using vector_default_alloc = std::vector<T>;

int main()
{
std::cout << typeid(convert_container<vector_default_alloc>::type).name();
}

我现在无法在 MSVC 上进行测试,但英特尔接受了它,而且我看不出这个变体无效的原因。

关于c++ - 使用模板模板参数时是否需要显式列出默认参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24017466/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com