GCC模板参数推断/替换失败

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

下面的代码在MSVC上编译但在GCC上失败(4.6.3)。为什么它会失败,我该怎么做才能解决它?

#include <array>

class Foo {
public:
    template<typename T, int N>
    operator std::array<T, N>() const {
        return std::array<T, N>();
    }
};

int main(){
    Foo val;

    // both of the following lines fail on GCC with error:
    //    "no matching function call...", ultimately with a note:
    //    "template argument deduction/substitution failed"
    auto a = val.operator std::array<int, 2>();
    static_cast<std::array<int, 2>>(val);

    return 0;
}

编辑:但是,下面的代码确实编译(在两个编译器上),尽管为int的模板参数传入了std::array

template<int N, typename T>
struct Bar {
    std::array<T, N> buf;
};

int main()
{
    auto x = Bar<3, double>();
    return 0;
}
c++ templates gcc compiler-errors c++14
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.