使用参考模板参数的部分特化无法在 VS2005 中编译
我的代码归结为以下内容:
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
到 const Foo
(通过指针而不是引用传递 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,VS2005 使用了
Baz
的第一个模板规范,这个结构确实不包含名为
func
的成员。看起来 VS2005 没有正确推导模板参数。For me it looks like VS2005 uses the first template specification of
Baz
This struct does indeed not contain a member named
func
. Looks like VS2005 doesn't deduce the template parameters correctly.