是否可以在模板中 typedef 指向外部“C”函数类型的指针?
我想向模板添加一个公共 typedef
,作为指向使用“C”语言链接的函数采用一个参数的指针。
我尝试过:
extern "C" {
template <typename return_t_, typename arg1_t_>
struct test
{
typedef return_t_ (*C_fun1_t)(arg1_t_);
};
}
和:
template <typename return_t_, typename arg1_t_>
struct test
{
extern "C" {
typedef return_t_ (*C_fun1_t)(arg1_t_);
}
};
和:
template <typename return_t_, typename arg1_t_>
struct test
{
extern "C" typedef return_t_ (*C_fun1_t)(arg1_t_);
};
没有成功。
我想要完成的事情可能吗?
I want to add a public typedef
to a template for a pointer to a function-taking-one-argument that uses "C" language linkage.
I tried:
extern "C" {
template <typename return_t_, typename arg1_t_>
struct test
{
typedef return_t_ (*C_fun1_t)(arg1_t_);
};
}
And:
template <typename return_t_, typename arg1_t_>
struct test
{
extern "C" {
typedef return_t_ (*C_fun1_t)(arg1_t_);
}
};
And:
template <typename return_t_, typename arg1_t_>
struct test
{
extern "C" typedef return_t_ (*C_fun1_t)(arg1_t_);
};
without success.
Is what I am trying to accomplish possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C++03,§7.5p4:
不幸的是,在当前的 C++ 中您根本无法做到这一点。该文本在最新的 C++0x 草案中没有变化,但“模板 typedefs”也许能够完成它。
C++03, §7.5p4:
Unfortunately, you simply can't do this in current C++. This text is unchanged in the latest C++0x draft, but "template typedefs" may be able to accomplish it.
考虑
boost::function
或 STL 函数对象的typedef
...而且由于一些非常明显的原因,您也无法在 extern“C”块内定义模板如果你想一想。Consider
typedef
of aboost::function
or the STL function objects ... also you can't define a template inside a extern "C" block for some quite obvious reasons if you think about it.如果我从你的 typedef 中省略
extern "C"
,一切似乎对我来说都工作得很好。也就是说,以下代码编译、链接和运行时不会出现警告、错误或问题:foo.c:
test.cpp:
对于 C++ 专家:我不知道 C 链接与 C++ 之间的区别。是否存在在这样的简单示例中不会出现的隐藏问题?
Everything seems to work fine for me if I just omit the
extern "C"
from your typedef. That is, the following compiles, links, and runs without warnings, errors, or problems:foo.c:
test.cpp:
For the C++ gurus: I don't know the finer points of what distinguishes C linkage from C++. Are there any hidden problems that wouldn't turn up in a simple example like this?