gpt4 book ai didi

c++ - 类外的成员模板函数定义无法构建 Visual Studio 2013

转载 作者:行者123 更新时间:2023-11-28 06:26:28 24 4
gpt4 key购买 nike

我已经在下面粘贴了我正在处理的一段复杂模板代码的最低限度。

 1  template <typename T>
2 class Base
3 : public T
4 {
5 public:
6 template <typename W, W& (T::ToImpl::*Func)()>
7 bool Foo();
8 };
9
10 template <typename T>
11 template <typename W, W& (T::ToImpl::*Func)()>
12 bool Base<T>::Foo()
13 {}
14
15 int
16 main()
17 {
18 return 0;
19 }

代码非常简单,所以我不做任何解释。我无法使用 Visual Studio 2013(又名 VC++12)编译此代码。它给出了以下错误:

main.cc(13): error C2244: 'Base<T>::Foo' : unable to match function definition to an existing declaration
definition
'bool Base<T>::Foo(W)'
existing declarations
'bool Base<T>::Foo(W)'

出于好奇,我尝试用 g++ (4.4.7) 编译上面的代码并且编译正常。

如果有人可以解释为什么代码无法在 Windows 上编译,我将不胜感激?修复会更甜蜜。 :)

最佳答案

这应该有效:

template <typename T>
struct to_impl
{
typedef typename T::ToImpl type;
};

template <typename T>
class Base
: public T
{
public:
template <typename W, W& (to_impl<T>::type::*Func)()>
bool Foo();
};

template <typename T>
template <typename W, W& (to_impl<T>::type::*Func)()>
bool Base<T>::Foo()
{
}

尽管在 Base 中实现 Foo 会更容易:

template <typename T>
class Base
: public T
{
public:
template <typename W, W& (T::ToImpl::*Func)()>
bool Foo()
{
}
};

关于c++ - 类外的成员模板函数定义无法构建 Visual Studio 2013,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28444446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com