0

考虑(链接):

#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 ‘&gt;’ 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::ExtractAs, typedefs 等,但无济于事。

我在 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 ‘&gt;’ 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);
}
4

1 回答 1

2

你可以说:

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

或者

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

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

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

于 2013-07-05T19:37:38.520 回答