对于我的一个项目,我真正想要做的是(将其简化为最低限度);
struct Move
{
int src;
int dst;
};
struct MoveTree
{
Move move;
std::vector<MoveTree> variation;
};
Run Code Online (Sandbox Code Playgroud)
我必须承认,我认为不可能直接这样做,我认为MoveTree中的MoveTree矢量将被禁止.但无论如何我都试过了,它的效果非常好.我正在使用Microsoft Visual Studio 2010 Express.
这是便携式吗?这是好习惯吗?我有什么可担心的吗?
编辑:我问了第二个问题,希望能找到一个好方法.
在下面的示例中,A有一个成员typedef Instantiate导致实例化B<A>.
template<typename T>
struct B
{
typedef typename T::Before Before; // ok
typedef typename T::After After; // error: no type named 'After' in 'A<int>'
};
template<typename T>
struct A
{
typedef int Before;
typedef typename B<A>::After Instantiate;
typedef int After;
};
template struct A<int>; // instantiate A<int>
Run Code Online (Sandbox Code Playgroud)
我尝试过的所有编译器都报告说,虽然A::Before可见但A::After并非如此.这种行为是否符合标准?如果是这样,标准在哪里指定A在实例化期间哪些名称应该可见B<A>?
如果依赖名称"在模板实例化时查找",那么在由模板参数限定的名称场景中,这意味着什么T::After?
编辑:请注意,当A不是模板时,会发生相同的行为:
template<typename T>
struct B
{
typedef typename T::Before Before; // ok
typedef typename T::After …Run Code Online (Sandbox Code Playgroud)