显式实例化:为什么不起作用

时间:2018-07-28 19:09:07

标签: c++11 instantiation

以下是示例code

在我的标题中:

class SomeClass:
{
template <int N, typename ExtractedType>
            bool ExtractDataFromArray(std::array<uint8_t, N> &ExtractFrom,
                                      uint8_t StartBit, uint8_t BitLen, ExtractedType &out);
}

在我的cpp中:

template <int N, typename ExtractedType>
bool SomeClass::ExtractDataFromArray(std::array<uint8_t, N> &ExtractFrom,
                                               uint8_t StartBit, uint8_t BitLen, ExtractedType &out) {
...<implementation for all>        }

// instantiation for used later:
        template bool SomeClass::ExtractDataFromArray<8, int>
            (std::array<uint8_t, 8>, uint8_t, uint8_t, int &);

为什么拒绝为对ExtractDataFromArray实例化<8, int>

Error是:

template-id ‘ExtractDataFromArray<8, int>’ for ‘bool SomeClass::ExtractDataFromArray(std::array<unsigned char, 8ul>, uint8_t, uint8_t, int&)’ does not match any template declaration
         template bool SomeClass::ExtractDataFromArray<8, int>
                       ^

1 个答案:

答案 0 :(得分:0)

尽管样本已被更改为多种类型,但原始答案仍然解决了其中一个问题-在您的完整专业领域中将template更改为template<>。还总是提到编译器-在gcc抱怨的情况下,跳过<>实际上以某种方式使用Visual Studio 2017进行编译。

此外,正如现在更改的示例中@ Irm29所发现的那样,您具有错误的签名,您缺少第一个参数的引用。应该是std::array<uint8_t, 8>&

#include <iostream>
#include <array>


class SomeClass
{
template <int N, typename ExtractedType>
bool ExtractDataFromArray(std::array<uint8_t, N> &ExtractFrom,
                          uint8_t StartBit, uint8_t BitLen, ExtractedType &out);
};

template <int N, typename ExtractedType>
bool SomeClass::ExtractDataFromArray(std::array<uint8_t, N> &ExtractFrom,
                                               uint8_t StartBit, uint8_t BitLen, ExtractedType &out) 
{
    return true;
}

template<>
bool SomeClass::ExtractDataFromArray<8, int>(std::array<uint8_t, 8>&, uint8_t, uint8_t, int &)
{
    return false;
}