如何简化C ++中的嵌套名称说明符?

假设我有以下代码:

template<bool t>
class A{
  class B{
    class C{
      public:
        void foo();
    };
  };
};

template<bool t>
void A<t>::B::C::foo() {
// some code
}

在编写此函数foo()的定义时,我希望避免编写太长的嵌套名称说明符A<t>::B::C::,而应使用类似aShortAlias<t>::foo()的名称。用C ++可以做到吗?

显然使用using aShrtAlias<t> = typename A<t>::B::C无效。而且我真的不希望使用#define作为替代方法,因为它只能进行文本替换(也许这里有#define的合理性吗?)。

daneee 回答:如何简化C ++中的嵌套名称说明符?

只需使用using

template <bool T>
using aShortAlias = A<T>::B::C
,

虽然您可以创建别名:

template<bool T>
using X = typename A<T>::B::C;

您不能在声明中使用该别名作为说明符:

template<bool T>
void X<T>::foo()   // not allowed; doesn't compile
{
   ...
}

Afaik,您需要在声明中使用完全限定的名称。

,

我认为您能做的最好的是:

template <bool T> using aShortAlias = typename A<T>::B::C;

template<> void aShortAlias<false>::foo() {
}
template<> void aShortAlias<true>::foo() {
}
本文链接:https://www.f2er.com/3062844.html

大家都在问