使用参考模板参数的部分特化无法在 VS2005 中编译

发布于 2024-09-02 02:34:44 字数 1314 浏览 2 评论 0原文

我的代码归结为以下内容:

template <typename T> struct Foo {};
template <typename T, const Foo<T>& I> struct FooBar {};

////////

template <typename T> struct Baz {};

template <typename T, const Foo<T>& I>
struct Baz< FooBar<T,I> >
{
 static void func(FooBar<T,I>& value);
};

////////

struct MyStruct
{
 static const Foo<float> s_floatFoo;
};

// Elsewhere: const Foo<float> MyStruct::s_floatFoo;

void callBaz()
{
 typedef FooBar<float, MyStruct::s_floatFoo> FloatFooBar;
 FloatFooBar myFloatFooBar;
 Baz<FloatFooBar>::func(myFloatFooBar);
}

这在 GCC 下编译成功,但是,在 VS2005 下,我得到:

error C2039: 'func' : is not a member of 'Baz<T>'
        with
        [
            T=FloatFooBar
        ]
error C3861: 'func': identifier not found

但是,如果我更改 const Foo& Iconst Foo* I (通过指针而不是引用传递 I),并将 FloatFooBar 定义为:

typedef FooBar<float, &MyStruct::s_floatFoo> FloatFooBar;

GCC 和 VS2005 都很高兴。

这是怎么回事?这是 VS2005 与 GCC 处理方式不同的某种微妙的模板替换失败,还是编译器错误?

(最奇怪的事情:我以为今天早上我已经在 VS2005 中运行了上面的代码。但那是在我早上喝咖啡之前。我现在不能完全确定我没有受到某种咖啡因的影响-渴望引起的谵妄...)

I have code that boils down to the following:

template <typename T> struct Foo {};
template <typename T, const Foo<T>& I> struct FooBar {};

////////

template <typename T> struct Baz {};

template <typename T, const Foo<T>& I>
struct Baz< FooBar<T,I> >
{
 static void func(FooBar<T,I>& value);
};

////////

struct MyStruct
{
 static const Foo<float> s_floatFoo;
};

// Elsewhere: const Foo<float> MyStruct::s_floatFoo;

void callBaz()
{
 typedef FooBar<float, MyStruct::s_floatFoo> FloatFooBar;
 FloatFooBar myFloatFooBar;
 Baz<FloatFooBar>::func(myFloatFooBar);
}

This compiles successfully under GCC, however, under VS2005, I get:

error C2039: 'func' : is not a member of 'Baz<T>'
        with
        [
            T=FloatFooBar
        ]
error C3861: 'func': identifier not found

However, if I change const Foo<T>& I to const Foo<T>* I (passing I by pointer rather than by reference), and defining FloatFooBar as:

typedef FooBar<float, &MyStruct::s_floatFoo> FloatFooBar;

Both GCC and VS2005 are happy.

What's going on? Is this some kind of subtle template substitution failure that VS2005 is handling differently to GCC, or a compiler bug?

(The strangest thing: I thought I had the above code working in VS2005 earlier this morning. But that was before my morning coffee. I'm now not entirely certain I wasn't under some sort of caffeine-craving-induced delirium...)

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

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

发布评论

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

评论(1

ま柒月 2024-09-09 02:34:44

对我来说,VS2005 使用了 Baz 的第一个模板规范,

template <typename T> struct Baz {};

这个结构确实不包含名为 func 的成员。看起来 VS2005 没有正确推导模板参数。

For me it looks like VS2005 uses the first template specification of Baz

template <typename T> struct Baz {};

This struct does indeed not contain a member named func. Looks like VS2005 doesn't deduce the template parameters correctly.

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