类模板的成员模板函数上的enable_if

发布于 2024-12-21 14:53:21 字数 781 浏览 4 评论 0原文

这似乎是 MSVC10 中的一个错误?

#include <type_traits>

template<int j>
struct A{
    template<int i>
    typename std::enable_if<i==j>::type
        t(){}
};

int main(){
    A<1>().t<1>();  //error C2770
}

错误 C2770:无效的显式 template_or_generic 参数“enable_if::type A::t(void)”。

编译如下:

#include <type_traits>

template<class j>
struct A{
    template<class i>
    typename std::enable_if<std::is_same<i,j>::value>::type
        t(){}
};

template<unsigned int j>
struct B{
    template<unsigned int i>
    typename std::enable_if<i==j>::type
        t(){}
};

int main(){
    A<int>().t<int>();
    B<1>().t<1>();
}

This seams to be a bug in MSVC10?

#include <type_traits>

template<int j>
struct A{
    template<int i>
    typename std::enable_if<i==j>::type
        t(){}
};

int main(){
    A<1>().t<1>();  //error C2770
}

error C2770: invalid explicit template_or_generic argument(s) "enable_if::type A::t(void)".

The following compiles:

#include <type_traits>

template<class j>
struct A{
    template<class i>
    typename std::enable_if<std::is_same<i,j>::value>::type
        t(){}
};

template<unsigned int j>
struct B{
    template<unsigned int i>
    typename std::enable_if<i==j>::type
        t(){}
};

int main(){
    A<int>().t<int>();
    B<1>().t<1>();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷血 2024-12-28 14:53:21

这似乎是 MSVC2010 方面的一些奇怪行为,它无法确定您是否使用 <1> 。作为模板参数是基于 int 的模板的实例化。

当我编译上面的代码时,我收到以下详细错误:

    error C2770: invalid explicit template argument(s) for 
    'std::enable_if<i==1>::type A<j>::t(void)'
    with
    [
        j=1
    ]
    d:\programming\stackoverflow\stackoverflow\stackoverflow.cpp(11) : 
    see declaration of 'A<j>::t'
    with
    [
        j=1
    ]

如果将 1 值交换为 0,您会发现它仍然不起作用,但如果您使用任何其他有效的 int,模板似乎编译得相当愉快。

我不完全确定为什么会发生这种情况,但是您可以通过使用 const ints 来表示模板参数来使此代码工作:

    template<int j>
    struct A{
        template<int i>
        typename std::enable_if<i == j>::type
            t(){}
    };

    int main(){

        const int j = 1;
        const int i = 1;

        A<j>().t<i>();   //now compiles fine
    }

基于此,我的怀疑是编译器发现 0 和 1 的使用在出现时不明确到模板实例化。希望这里的解决方法对通过谷歌偶然发现这个问题的人有帮助......

This appears to be some strange behaviour on the part of MSVC2010 where it is unable to make a determination as to whether your use of <1> as a template parameter is an instantiation of an int-based template.

When I compile your code above, I get the following, verbose error:

    error C2770: invalid explicit template argument(s) for 
    'std::enable_if<i==1>::type A<j>::t(void)'
    with
    [
        j=1
    ]
    d:\programming\stackoverflow\stackoverflow\stackoverflow.cpp(11) : 
    see declaration of 'A<j>::t'
    with
    [
        j=1
    ]

If you swap your 1 values out for 0, you find it still doesn't work, but if you use any other valid int, the template appears to compile quite happily.

I'm not entirely sure why this is happening, but you can make this code work by using const ints to represent the template parameters:

    template<int j>
    struct A{
        template<int i>
        typename std::enable_if<i == j>::type
            t(){}
    };

    int main(){

        const int j = 1;
        const int i = 1;

        A<j>().t<i>();   //now compiles fine
    }

My suspicion, based on this, is that the compiler finds the use of 0 and 1 ambiguous when it comes to template instantiation. Hopefully the workaround here is helpful to someone stumbling across this through google...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文