首页 > 解决方案 > CTAD 和带有参数包成员的模板构造函数

问题描述

尝试将通用引用与 CTAD 和参数包一起使用,我尝试了类似下面的方法,但它无法在 MinGW GCC 上编译。我不明白为什么。这是示例代码:

#include <utility>

template<typename... Ts>
struct Subtest
{
    Subtest(Ts...) {}
};

template<typename... Ts>
struct Test
{
    template<typename... Us>
    Test( Us&&... values1 )
    : values( std::forward<Us>( values1 )... )
    {}

    Subtest<Ts...> values;
};

template<typename... Ts>
Test(Ts&&...) -> Test<std::remove_reference_t<Ts>...>;


int main()
{
    Test t(12, 14);

    return 0;
}

这些是错误:

main.cpp: In instantiation of 'Test<Ts>::Test(Us&& ...) [with Us = {int, int}; Ts = {}]':
main.cpp:26:18:   required from here
main.cpp:14:46: error: no matching function for call to 'Subtest<>::Subtest(int, int)'
   14 |     : values( std::forward<Us>( values1 )... )
      |                                              ^
main.cpp:6:5: note: candidate: 'Subtest<Ts>::Subtest(Ts ...) [with Ts = {}]'
    6 |     Subtest(Ts...) {}
      |     ^~~~~~~
main.cpp:6:5: note:   candidate expects 0 arguments, 2 provided
main.cpp:4:8: note: candidate: 'constexpr Subtest<>::Subtest(const Subtest<>&)'
    4 | struct Subtest
      |        ^~~~~~~
main.cpp:4:8: note:   candidate expects 1 argument, 2 provided
main.cpp:4:8: note: candidate: 'constexpr Subtest<>::Subtest(Subtest<>&&)'
main.cpp:4:8: note:   candidate expects 1 argument, 2 provided

标签: c++parameter-packctad

解决方案


推荐阅读