繁体   English   中英

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

[英]GCC template argument deduction/substitution failed

下面的代码在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;
}

编辑:但是,尽管传入了std::array的template参数的int ,但以下代码确实进行了编译(在两个编译器上)。

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

int main()
{
    auto x = Bar<3, double>();
    return 0;
}

如果您阅读错误消息的全文,则编译器会抱怨,因为模板类中N的类型是int ,而std::array的第二个参数是std::size_t ,它是一个unsigned long int 。你的系统。

更改模板的声明以使用std::size_t N将解决此问题。

MSVC可能没有抱怨,可能是因为它认识到值“ 2”适用于两种情况,或者是由于编译器错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM