我有一个非常依赖于模板的C++库(.lib)。我正在为这个库创建一个C++包装器,以便在.dll外部公开它的函数。这些公开的函数将分别由Java和C#库通过JNA/JNI和PInvoke使用。
我从了解到,不可能导出模板。
有没有人对包装C++模板的最佳方法有很好的指导,这样既能保留它们的灵活性,又能将其导出?
例如,典型的C++函数如下所示:
template <class A, class B>
inline
A
do_something(A first, A last,
B result )
{
/* implementation *
我很难理解C++模板模板参数的语法。我理解为什么它们是有用的,根据的出色描述,我只是发现它们的语法很难理解。取自上述网站的两个示例(还有其他示例):
template <typename T, template <typename> class Cont>
class Stack;
和
template <template <typename,typename> class Cont>
class Wrapper3;
显然,如果不了解这种语法背后的原理,就不可能对这些声明进行泛化。记忆更难,而且似乎也没有多大帮助。
编辑:我意识到我对一个问题的尝
在C++中,我有一个关于特定模板友谊的问题。在C++ Primer一书中,具体的模板友谊如下所示:
template <class T> class Foo3;
template <class T> void templ_fcn3(const T&);
template <class Type> class Bar {
// each instantiation of Bar grants access to the
// version of Foo3 or templ_fcn3 instantiated with the
我有一个问题(标题中也有):在C++中是否可能像read<type>()一样创建泛型函数?
我已经试过了
template <typename T>
T read() {
T res;
if (typeid(T) == typeid(int))
{
// some stuff
}
else if (/*some other type check*/) {} // etc.
}
当我试图编译它时,它抛出了:Invalid conversion from **** to ****。我知道它为什么要抛出这样的异常,但是
我正试图使用汇编程序代码-- ARMv7-a来实现C++函数。现在我遇到一个程序,我不知道如何在程序集中实现C++函数模板。我尝试用-S -O1标志编译源代码,以查看生成的程序集,但无法理解。有谁能简单地告诉我C++模板是如何被转换成组装代码的?只需使用以下简单函数作为示例:
template<typename T>
T f(T a) {
return a + 1;
}
如果您发现任何其他函数更容易进行解释,请这样做。谢谢!
我用的是gcc 4.7.2
使用以下代码:
#include <iostream>
using namespace std;
int main()
{
string x="hello";
int y=1;
x=x+y;
cout<<x;
return 0;
}
我得到了错误:
g++ test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:8:6: error: no match for ‘operator+’ in ‘x + y’
如果我创建了一个指向基成员的指针,我通常可以将它转换为指向派生成员的指针,但当在下面的Buzz这样的模板中使用时就不能了,因为第一个模板参数会影响第二个模板参数。我是在与编译器bug作斗争,还是标准真的要求这样做不起作用?
struct Foo
{
int x;
};
struct Bar : public Foo
{
};
template<class T, int T::* z>
struct Buzz
{
};
static int Bar::* const workaround = &Foo::x;
int main()
{
// This