显式模板实例化问题

Problem with Explicit Template Instantiation

我无法编译以下带有显式模板实例化的代码。

我在这一行遇到错误:

std::map<uint8_t, std::string> DW_enumDescription::descMap = std::map<uint8_t, std::string>

用 clang,错误是:

error: no member named 'descMap' in 'O::Data::DW_enumDescription'

对于 G++ 是:

error: ISO C++ does not permit 'O::Data::EnumDescription::descMap' to be defined as 'O::Data::DW_enumDescription::descMap' [-fpermissive]

这里我一直使用相同的命名空间Data,甚至在我的原始代码中我使用了两个独立的命名空间;但是,即使进行了这种简化,我也会遇到同样的错误。

#include <cstdint>
#include <map>
#include <string>

namespace O
{

    namespace Data
    {

        template<typename E             /* enum to describe */
                ,typename T = uint8_t   /* index type */
                >
        class EnumDescription
        {
        public:
            /// Static map to store the descriptions
            static std::map<T, std::string> descMap;
        };
    }
}

namespace O
{
    namespace Data
    {

        enum struct DW : uint8_t
        {
            DW_Unknown = 0,
            DW_TS_1    = 1,
            DW_TS_2    = 2,
            DW_DS_1    = 3,
            DW_Dev     = 4,
            enumSize   = 5
        };

        class DW_enumDescription : public Data::EnumDescription<DW, uint8_t> {};

    }
}


// ===================== Explicit Template Instantiation ======================
namespace O
{
    template class Data::EnumDescription<Data::DW, uint8_t>;
}
// ============================================================================

namespace O
{
    namespace Data
    {
        std::map<uint8_t, std::string> DW_enumDescription::descMap = std::map<uint8_t, std::string> // ERROR
        {
            { static_cast<uint8_t>(DW::DW_Unknown), "Unknown" },
            { static_cast<uint8_t>(DW::DW_TS_1),    "DW_TS_1" },
            { static_cast<uint8_t>(DW::DW_TS_2),    "DW_TS_2" },
            { static_cast<uint8_t>(DW::DW_DS_1),    "DW_DS_1" },
            { static_cast<uint8_t>(DW::DW_Dev),     "DW_Dev"  },
        };
    }
}

descMapEnumDescription<DW, uint8_t> 的成员,因此,修复:

namespace O
{
    namespace Data
    {
        template<> std::map<uint8_t, std::string> EnumDescription<DW, uint8_t>::descMap =
        {
            { static_cast<uint8_t>(DW::DW_Unknown), "Unknown" },
            { static_cast<uint8_t>(DW::DW_TS_1),    "DW_TS_1" },
            { static_cast<uint8_t>(DW::DW_TS_2),    "DW_TS_2" },
            { static_cast<uint8_t>(DW::DW_DS_1),    "DW_DS_1" },
            { static_cast<uint8_t>(DW::DW_Dev),     "DW_Dev"  },
        };
    }
}