“未在此范围内声明”错误解析公共派生的基类模板中的函数模板的名称

时间:2013-07-05 19:33:35

标签: c++

考虑(link):

#include <cstdlib>
#include <cassert>

#pragma pack (1)

template <size_t Width>
class Base
{
public:
    char mData [Width];
    template <typename Field> Field ExtractAs () const
    {
        return *reinterpret_cast <Field> (mData);
    }
};

template <typename FieldVal>
class IntegralField
:
    public Base <sizeof (FieldVal)>
{
public:
    FieldVal GetVal () const
    {
        return ExtractAs <FieldVal> ();
    }
};

int main()
{
    char raw[4] = {0x11, 0x22, 0x33, 0x44};
    typedef IntegralField <uint32_t> UInt32Field;
    const UInt32Field& field =
        *reinterpret_cast <const UInt32Field*> (raw);
    const uint32_t extracted = field.GetVal();
    assert (extracted == 0x44332211);
}

电话:

return ExtractAs <FieldVal> ();

无法在g ++ 4.7.2下编译:

main.cpp: In member function ‘FieldVal IntegralField<FieldVal>::GetVal() const’:
main.cpp:25:16: error: ‘ExtractAs’ was not declared in this scope
main.cpp:25:35: error: expected primary-expression before ‘>’ token
main.cpp:25:38: error: expected primary-expression before ‘)’ token
main.cpp: In function ‘int main()’:
main.cpp:32:28: error: ‘uint32_t’ was not declared in this scope
main.cpp:32:36: error: template argument 1 is invalid
main.cpp:32:49: error: invalid type in declaration before ‘;’ token
main.cpp:35:11: error: ‘uint32_t’ does not name a type
main.cpp:36:5: error: ‘extracted’ was not declared in this scope
ninja: build stopped: subcommand failed.

我尝试了许多技巧,包括using Base::ExtractAstypedef等,但无济于事。

我想在C ++ 03中做些什么?如何从派生类模板成员函数中调用基类模板中的函数模板?请注意,我不能使用C ++ 11。

修改:当我重新定义GetVal以更明确地了解类型时:

  FieldVal GetVal () const
    {
        static const size_t fieldSize = sizeof (FieldVal);
        typedef Base <fieldSize> MyBase;
        typedef FieldVal MyField;
        return MyBase::ExtractAs <MyField> ();
    }

我仍然得到:

error: expected primary-expression before ‘>’ token

开:return MyBase::ExtractAs <MyField> ();

编辑:以下是最终的有效代码:

#include <cstdlib>
#include <cassert>
#include <stdint.h>

#pragma pack (1)

template <size_t Width>
class Base
{
public:
    char mData [Width];
    template <typename Field> Field ExtractAs () const
    {
        return *reinterpret_cast <const Field*> (mData);
    }
};

template <typename FieldVal>
class IntegralField
:
    public Base <sizeof (FieldVal)>
{
public:
    FieldVal GetVal () const
    {
        return this->template ExtractAs<FieldVal>();
    }
};

int main()
{
    char raw[4] = {0x11, 0x22, 0x33, 0x44};
    typedef IntegralField <uint32_t> UInt32Field;
    const UInt32Field& field =
        *reinterpret_cast <const UInt32Field*> (raw);
    const uint32_t extracted = field.GetVal();
    assert (extracted == 0x44332211);
}

1 个答案:

答案 0 :(得分:2)

你可以说:

return this->template ExtractAs<FieldVal>();

或者

return Base<sizeof(FieldVal)>::template ExtractAs<FieldVal>();

由于您位于类模板中且基础也是模板特化,因此基本成员的名称不会自动注入到派生模板中。 (如果你专攻Base,请考虑会发生什么!)

通过限定名称或使用this->,您可以使整个名称相关,因此在第一阶段不会导致错误。此外,由于名称ExtractAs是相关的(作为模板的嵌套名称),因此您必须将其歧义为template