具有多个包的参数包匹配规则

问题描述 投票:6回答:2

我正在尝试编写一个函数,它使用参数包和一些标准匹配规则来获取另一个函数。举个例子:

template <typename... TListElems, typename... TVectorElems>
void goal(void (*fn)(std::list<TListElems>..., std::vector<TVectorElems>...));

为了消除TListElemsTVectorElems的歧义,我添加了一些std::tuple<T...>*,因此调用者可以是明确的:

template <typename... TListElems, typename... TVectorElems>
void foo(std::tuple<TListElems...>*,
         std::tuple<TVectorElems...>*,
         void (*)(std::list<TListElems>..., std::vector<TVectorElems>...))
{
    // blah blah blah
}

void bar(std::list<int>, std::list<unsigned>, std::vector<float>, std::vector<double>)
{
    // blah blah blah
}

int main()
{
    foo((std::tuple<int, unsigned>*) nullptr,
        (std::tuple<float, double>*) nullptr,
        &bar);
}

Clang愉快地以我期望的方式编译它,而g ++(7.2.1)给出了编译错误:

matching.cpp: In function ‘int main()’:
matching.cpp:20:13: error: no matching function for call to ‘foo(std::tuple<int, unsigned int>*, std::tuple<float, double>*, void (*)(std::list<int>, std::list<unsigned int>, std::vector<float>, std::vector<double>))’
         &bar);
             ^
matching.cpp:6:6: note: candidate: template<class ... TListElems, class ... TVectorElems> void foo(std::tuple<_Tps ...>*, std::tuple<_Elements ...>*, void (*)(std::list<TListElems>..., std::vector<TVectorElems>...))
 void foo(std::tuple<TListElems...>*,
      ^~~
matching.cpp:6:6: note:   template argument deduction/substitution failed:
matching.cpp:20:13: note:   mismatched types ‘std::vector<TVectorElems>’ and ‘std::list<int>’
         &bar);
             ^

main,我希望foo的调用推断TListElems<int, unsigned>TVectorElems<float, double>,导致fnvoid (*)(std::list<int>, std::list<unsigned>, std::vector<float>, std::vector<double>)类型(当只有一个包或者我手动写过载时,事情的运作方式)。

§14.8.2.5/ 10是最接近明确阻止foo示例工作的标准:

[注意:函数参数包只能出现在参数声明列表(8.3.5)的末尾。 - 尾注]

std::list<TListElems>...fn位似乎会违反这个音符,但这并不完全清楚。

问题是:谁是对的? GCC,Clang还是其他什么?

c++ c++11 templates language-lawyer variadic-templates
2个回答
3
投票

我觉得clang就在这里。

void (*)(std::list<TListElems>..., std::vector<TVectorElems>...)TListElems...a non-deduced context,它使TVectorElems...a non-deduced context。但是两个参数包都可以从两个元组指针参数中推导出来,并且预计它也可以在这里只是use that deduction result

我提交了gcc bug 83542


2
投票

您可以使用非可推导类型的两个编译器开心:

template <typename T>
struct non_deducible {
    using type = T;  
};

template <typename T> using non_deducible_t = typename non_deducible<T>::type;


template <typename... TListElems, typename... TVectorElems>
void foo(std::tuple<TListElems...>*,
         std::tuple<TVectorElems...>*,
         void (*)(non_deducible_t<std::list<TListElems>>...,
                  non_deducible_t<std::vector<TVectorElems>>...))
{
    // blah blah blah
}

Demo

© www.soinside.com 2019 - 2024. All rights reserved.