Home > OS >  Declaration of template specialization on a template function
Declaration of template specialization on a template function

Time:05-22

I'm getting undefined reference to ´long long fromBigEndin<long long>(unsigned char*)´ for a template specialization.

See code here: https://onlinegdb.com/AagKTQJ2B

I have this structure:

util.h

template <class T>
T fromBigEndin(uint8_t *buf);

template <>
int64_t fromBigEndin<int64_t>(uint8_t *buf);
template <>
long long fromBigEndin<long long>(unsigned char *buf);

util.cpp

template<class T>
T fromBigEndin(uint8_t *buf) {
    T number = 0;
    uint8_t nBytes = sizeof(T);
    uint8_t i;

    for (i = 0; i < nBytes; i  = 1) {
        number  = buf[i] << (16 * (nBytes - i - 1));
    }

    return number;
}

headerImpl.h

#include "util.h"

void handleOpenSession(uint8_t *data) {
    uint8_t *uid = (uint8_t *)malloc(8);
    memcpy(uid, data   1, 8);
    int64_t uidNbr = fromBigEndin<int64_t>(uid);
}

From @AnoopRana response, putting the implementation in header file works, I would like to know if it is possible to put the implementation in a separate file.

Any idea on how could I force the compilation of fromBigEndin<int64_t>()?

I've also tried to move the specializations to util.cpp but doesn't work either.

The code itself works when in a single file and with different declarations:

CodePudding user response:

Method 1

You need to put the implementation of the function template into the header file itself. So it would look something like:

util.h

#pragma once  //include guard added
template <class T>
T fromBigEndin(uint8_t *buf);

//definition
template <> int64_t fromBigEndin<int64_t>(uint8_t *buf)
{
    return 53;
}
//definition 
template <> long long fromBigEndin<long long>(unsigned char *buf)
{ 
    return 54;
}


//implementation in header file itself
template<class T>
T fromBigEndin(uint8_t *buf) {
    T number = 0;
    uint8_t nBytes = sizeof(T);
    uint8_t i;

    for (i = 0; i < nBytes; i  = 1) {
        number  = buf[i] << (16 * (nBytes - i - 1));
    }

    return number;
}

headerImpl.h

#pragma once
#include "util.h"

void handleOpenSession(uint8_t *data) {
    //add code here 
}

main.cpp

#include <iostream>
#include "util.h"
int main()
{
    
    return 0;
}

Working demo

Method 2

Here we make use of explicit template instantiations.

util.h

#pragma once
#include <cstdint>
template <class T>
T fromBigEndin(uint8_t *buf);


headerImpl.h

#pragma once
#include "util.h"


void handleOpenSession(uint8_t *data) {
    uint8_t *uid = (uint8_t *)malloc(8);
    memcpy(uid, data   1, 8);
    int64_t uidNbr = fromBigEndin<int64_t>(uid);
}

util.cpp

#include "util.h"

template<class T>
T fromBigEndin(uint8_t *buf) {
    T number = 0;
    uint8_t nBytes = sizeof(T);
    uint8_t i;

    for (i = 0; i < nBytes; i  = 1) {
        number  = buf[i] << (16 * (nBytes - i - 1));
    }

    return number;
}

//no angle brackets used here
template int64_t fromBigEndin<int64_t>(uint8_t *buf);
template long long fromBigEndin<long long>(unsigned char *buf);

main.cpp


#include <iostream>

#include "util.h"
int main()
{
    
    return 0;
}

Working demo

  • Related