英文:
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 "C" {
#endif
#include "stdint.h"
void fooInt(int val);
void fooFloat(float val);
#ifdef __cplusplus
}
#endif
and cpp file:
// lib.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);
}
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 "C" {
#endif
#include "stdint.h"
struct COptionalUint16_t;
#ifdef __cplusplus
}
#endif
source file
#include "lib.h"
#include <iostream>
#include <typeinfo>
template <typename T>
struct COptional{
T value;
bool isSet;
};
using COptionalUint16_t = COptional<uint16_t>;
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 <cstdint>
"#include <cstdint>
template <typename T>
template <typename T>
struct COptional {
T value;
bool isSet;
};
"结构COptional {
T value;
bool isSet;
};"
using COptionalUint16_t = COptional<uint16_t>;
"using COptionalUint16_t = COptional<uint16_t>;"
#else
#else
#include <stdint.h>
"#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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论