Exporting struct template instantiation to C code from inside C++ library

huangapple go评论70阅读模式
英文:

Exporting struct template instantiation to C code from inside C++ library

问题

当混合使用C和C++时,其中C++是一个库而C是主应用程序时,可以进行以下操作:

lib.h:

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include "stdint.h"

void fooInt(int val);
void fooFloat(float val);

#ifdef __cplusplus
}
#endif

cpp文件:

#include "lib.h"
#include <iostream>
#include <typeinfo>

template <typename T>
void foo(T val) {
    std::cout << typeid(val).name() << std::endl;
}

void fooInt(int val) {
    foo(val);
}

void fooFloat(float val) {
    foo(val);
}

这允许我基于模板生成一些C函数。但是,我想知道是否有办法对结构体执行相同的操作?

头文件:

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include "stdint.h"

struct COptionalUint16_t;

#ifdef __cplusplus
}
#endif

源文件:

#include "lib.h"
#include <iostream>
#include <typeinfo>

template <typename T>
struct COptional{
    T value;
    bool isSet;
};

using COptionalUint16_t = COptional<uint16_t>;

显然,最后一个示例不起作用。也许有人能建议一种解决方法吗?

英文:

When mixing C and C++, with C++ being a lib and C the main app, I can do following:

// lib.h
#pragma once

#ifdef __cplusplus
extern &quot;C&quot; {
#endif

#include &quot;stdint.h&quot;

void fooInt(int val);
void fooFloat(float val);

#ifdef __cplusplus
}
#endif

and cpp file:

// lib.cpp
#include &quot;lib.h&quot;
#include &lt;iostream&gt; 
#include &lt;typeinfo&gt;

template &lt;typename T&gt;
void foo(T val) {
    std::cout &lt;&lt; typeid(val).name() &lt;&lt; std::endl;
}

void fooInt(int val) {
    foo(val);
}

void fooFloat(float val) {
    foo(val);
}

Which allows me to generate a number of c function based on template. But I wonder is there a way to do the same for structs?

header:

#pragma once

#ifdef __cplusplus
extern &quot;C&quot; {
#endif

#include &quot;stdint.h&quot;

struct COptionalUint16_t;

#ifdef __cplusplus
}
#endif

source file

#include &quot;lib.h&quot;
#include &lt;iostream&gt; 
#include &lt;typeinfo&gt;

template &lt;typename T&gt;
struct COptional{
    T value;
    bool isSet;
};

using COptionalUint16_t = COptional&lt;uint16_t&gt;;

Obviously, the last example does not work. Maybe someone could suggest the way around that?

答案1

得分: 0

以下是翻译好的部分:

"Nothing can be done like with the functions. You can do

"什么都不能像函数那样做。你可以这样做"
```#pragma once

"#pragma once

#ifdef __cplusplus

#ifdef __cplusplus

#include &lt;cstdint&gt;

"#include <cstdint>

template &lt;typename T&gt;

template <typename T>

struct COptional {
    T value;
    bool isSet;
};

"结构COptional {
T value;
bool isSet;
};"
using COptionalUint16_t = COptional<uint16_t>;

"using COptionalUint16_t = COptional&lt;uint16_t&gt;;"
#else

#else

#include &lt;stdint.h&gt;

"#include <stdint.h>"
typedef struct {
uint16_t value;
bool isSet;
} COptionalUint16_t;

"typedef struct {
    uint16_t value;
    bool isSet;
} COptionalUint16_t;"

<details>
<summary>英文:</summary>

Nothing can be done like with the functions. You can do

#pragma once

#ifdef __cplusplus

#include <cstdint>

template <typename T>
struct COptional {
T value;
bool isSet;
};

using COptionalUint16_t = COptional<uint16_t>;

#else

#include <stdint.h>

typedef struct {
uint16_t value;
bool isSet;
} COptionalUint16_t;

#endif


</details>



# 答案2
**得分**: 0

用typedef代替'using',像这样:

```c
typedef COptional<uint16_t> myCoptional_uint16;

然后在C代码中:

myCoptional_uint16 myInstance;
英文:

instead of 'using' try typedef like this.

typedef COptional<uint16_t> myCoptional_uint16;

then in the c code

myCoptional_uint16 myInstance;

huangapple
  • 本文由 发表于 2023年6月8日 07:48:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定