专门化类模板构造函数

发布于 2024-08-29 15:50:22 字数 1239 浏览 3 评论 0原文

我正在搞乱模板专门化,并且在尝试根据所使用的策略专门化构造函数时遇到了问题。这是我试图开始工作的代码。

#include <cstdlib>
#include <ctime>

class DiePolicies {
public:
 class RollOnConstruction { };
 class CallMethod { };
};

#include <boost/static_assert.hpp>
#include <boost/type_traits/is_same.hpp>
template<unsigned sides = 6, typename RollPolicy = DiePolicies::RollOnConstruction>
class Die {
 // policy type check
 BOOST_STATIC_ASSERT(( boost::is_same<RollPolicy, DiePolicies::RollOnConstruction>::value ||
        boost::is_same<RollPolicy, DiePolicies::CallMethod>::value ));
 unsigned m_die;
 unsigned random() { return rand() % sides; }
public:
 Die();
 void roll() { m_die = random(); }
 operator unsigned () { return m_die + 1; }
};

template<unsigned sides>
Die<sides, DiePolicies::RollOnConstruction>::Die() : m_die(random()) { }
template<unsigned sides>
Die<sides, DiePolicies::CallMethod>::Die() : m_die(0) { }

...\main.cpp(29): 错误 C3860: 类模板名称后面的模板参数列表必须按照模板参数列表中使用的顺序列出参数 ...\main.cpp(29): 错误 C2976: 'Die' : 模板参数太少 ...\main.cpp(31): 错误 C3860: 类模板名称后面的模板参数列表必须按照模板参数列表中使用的顺序列出参数

这些是我在 Microsoft Visual Studio 2010 中遇到的错误。我想要么我无法找出专业化的正确语法,或者也许不可能以这种方式做到这一点。

I'm messing around with template specialization and I ran into a problem with trying to specialize the constructor based on what policy is used. Here is the code I am trying to get to work.

#include <cstdlib>
#include <ctime>

class DiePolicies {
public:
 class RollOnConstruction { };
 class CallMethod { };
};

#include <boost/static_assert.hpp>
#include <boost/type_traits/is_same.hpp>
template<unsigned sides = 6, typename RollPolicy = DiePolicies::RollOnConstruction>
class Die {
 // policy type check
 BOOST_STATIC_ASSERT(( boost::is_same<RollPolicy, DiePolicies::RollOnConstruction>::value ||
        boost::is_same<RollPolicy, DiePolicies::CallMethod>::value ));
 unsigned m_die;
 unsigned random() { return rand() % sides; }
public:
 Die();
 void roll() { m_die = random(); }
 operator unsigned () { return m_die + 1; }
};

template<unsigned sides>
Die<sides, DiePolicies::RollOnConstruction>::Die() : m_die(random()) { }
template<unsigned sides>
Die<sides, DiePolicies::CallMethod>::Die() : m_die(0) { }

...\main.cpp(29): error C3860: template argument list following class template name must list parameters in the order used in template parameter list
...\main.cpp(29): error C2976: 'Die' : too few template arguments
...\main.cpp(31): error C3860: template argument list following class template name must list parameters in the order used in template parameter list

Those are the errors I get in Microsoft Visual Studio 2010. I'm thinking either I can't figure out the right syntax for the specialization, or maybe it isn't possible to do it this way.

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

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

发布评论

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

评论(1

仙气飘飘 2024-09-05 15:50:22

您的构造函数不是模板函数。你应该专注于整个班级。

Your constructor is not a template function. You should specialize the whole class.

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