在模板功能上的模板专业化声明

发布于 2025-01-31 01:37:41 字数 1665 浏览 4 评论 0原文

我会得到对模板专业化的长长的frombigendin&lt long>(unsigned char*)的不确定的引用。

请参阅此处: https://onlinegdb.com/aagktqj2b

我有这个结构

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);
}

来自@Anooprana响应,将实现放在标题文件中,我想知道是否可以将实现进行实现在单独的文件中。

关于我如何强制对frombigendin&lt; int64_t&gt;()的汇编的想法?

我还试图将专业人士移至util.cpp,但也不起作用。

该代码本身在单个文件中和不同声明中起作用:

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:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一念一轮回 2025-02-07 01:37:41

方法1

您需要将功能模板的实现放入标题文件本身中。因此,它看起来像:

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;
}

工作演示

方法2

这里我们使用显式模板实例。

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;
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文