gpt4 book ai didi

c++ - 可变参数类型的部分模板特化和扩展到外部类型的可变参数包导致歧义

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:53 25 4
gpt4 key购买 nike

我正在尝试使用 g++-4.7 (20120228-1) 编译以下程序:

#include <cstdlib>
#include <tuple>

template<typename X> struct Y {};

template<typename T, size_t Level, size_t TermLevel> struct A;

// (B) dummy for T=tuple<int, Ts...> just to show it works for simple expansions
template<typename ... Ts, size_t Level, size_t TermLevel>
struct A<std::tuple<int, Ts...>, Level, TermLevel>
{
A<std::tuple<int, Ts...>, Level+1, TermLevel> value;
};

template<typename ... Ts, size_t Level>
struct A<std::tuple<int, Ts...>, Level, Level> {};


// (C) ambiguous partial specialization
template<typename ... Ts, size_t Level, size_t TermLevel>
struct A<std::tuple<Y<Ts>...>, Level, TermLevel>
{
A<std::tuple<Y<Ts>...>, Level+1, TermLevel> value;
};

template<typename ... Ts, size_t Level>
struct A<std::tuple<Y<Ts>...>, Level, Level> {};


int main(int argc, const char *argv[])
{
A<std::tuple<int, float, int>, 0, 5> tint;
A<std::tuple<Y<int>, Y<float>>, 0, 1> tn;
return 0;
}

这导致歧义如下:

g++-4.7 -g -O0 -std=c++0x    specialization_orig.cc   -o specialization_orig
specialization_orig.cc: In instantiation of 'struct A<std::tuple<Y<int>, Y<float> >, 0ul, 1ul>':
specialization_orig.cc:33:43: required from here
specialization_orig.cc:23:49: error: ambiguous class template instantiation for 'struct A<std::tuple<Y<int>, Y<float> >, 1ul, 1ul>'
specialization_orig.cc:21:8: error: candidates are: struct A<std::tuple<Y<Ts>...>, Level, TermLevel>
specialization_orig.cc:27:8: error: struct A<std::tuple<Y<Ts>...>, Level, Level>
specialization_orig.cc:23:49: error: 'A<std::tuple<Y<Ts>...>, Level, TermLevel>::value' has incomplete type
specialization_orig.cc:6:61: error: declaration of 'struct A<std::tuple<Y<int>, Y<float> >, 1ul, 1ul>'

这有点奇怪,因为可变参数扩展适用于参数包的简单扩展,但一旦可变参数包扩展为嵌套在其他模板类型中就会失败。

这仅仅是编译器的疯狂还是我做错了什么?

最佳答案

它看起来像一个 GCC 错误,因为如果用固定数量的参数替换参数包,歧义就会消失:http://ideone.com/6D4Fi .

这种歧义也可以使用 std::enable_if 来解决。

/* add "typename = void" anonymous parameter which enables individual partial
specializations by matching the void result in std::enable_if<>::type. */
template<typename T, size_t Level, size_t TermLevel, typename = void> struct A;

// No enable_if needed here: always enabled if the levels are equal.
template<typename ... Ts, size_t Level>
struct A<std::tuple<int, Ts...>, Level, Level, void> {}; // just pass void

// Use enable_if to disable in case the levels are equal.
template<typename ... Ts, size_t Level, size_t TermLevel>
struct A<std::tuple<Y<Ts>...>, Level, TermLevel,
typename std::enable_if< Level != TermLevel >::type >

关于c++ - 可变参数类型的部分模板特化和扩展到外部类型的可变参数包导致歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9788242/

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