C ++可变参数模板推论逻辑

问题描述 投票:3回答:1

我有简单的代码:

#include <iostream>

template<class... Types>
class A;

template<>
class A<> {
public:
    explicit A() {
        std::cout << "empty" << std::endl;
    }
};

template<class Head, class... Tail>
class A<Head, Tail...> {
public:
    explicit A(Head head, Tail ...tail) : tail_(tail...) {
        std::cout << head << std::endl;
    }

private:
    A<Tail...> tail_;
};


int main() {
    auto a = A(1, 2);
    return 0;
}

无法编译:

test2.cpp:27:20: error: class template argument deduction failed:
     auto a = A(1, 2);
                    ^
test2.cpp:27: confused by earlier errors, bailing out

可以通过添加扣除指南来解决,例如:

template<class... T>
A(T...) -> A<T...>;

我的问题是为什么编译器无法解决这种简单的情况?

templates g++ c++17 variadic
1个回答
0
投票

隐式推论指南仅从primary

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