Java有泛型,C++提供了一个非常强大的编程模型template.那么,C++和Java泛型有什么区别?
正如我所理解的那样,可以使用"export"关键字,以便可以通过头文件公开模板类或函数签名,并在库文件中抽象实际实现.
任何人都可以提供一个实用的示例程序,说明如何做到这一点?
使用此功能时是否有任何缺点或重点?
编辑:基于答案的后续问题.正如答案中所提到的,'export'在C++ 0x中已被弃用,即使对于C++ 03x也很少得到编译器的支持.鉴于这种情况,可以用什么方式隐藏lib文件中的实际实现,只是通过头文件公开声明,以便最终用户可以知道暴露的API的签名是什么,但是无法访问实现相同的源代码?
我正在尝试创建一个通用函数,从std :: vector中删除重复项.由于我不想为每个矢量类型创建一个函数,我想让它成为一个可以接受任何类型矢量的模板函数.这是我有的:
//foo.h
Class Foo {
template<typename T>
static void RemoveVectorDuplicates(std::vector<T>& vectorToUpdate);
};
//foo.cpp
template<typename T>
void Foo::RemoveVectorDuplicates(std::vector<T>& vectorToUpdate) {
for(typename T::iterator sourceIter = vectorToUpdate.begin(); (sourceIter != vectorToUpdate.end() - 1); sourceIter++) {
for(typename T::iterator compareIter = (vectorToUpdate.begin() + 1); compareIter != vectorToUpdate.end(); compareIter++) {
if(sourceIter == compareIter) {
vectorToUpdate.erase(compareIter);
}
}
}
}
//SomeOtherClass.cpp
#include "foo.h"
...
void SomeOtherClass::SomeFunction(void) {
std::vector<int> myVector;
//fill vector with values
Foo::RemoveVectorDuplicates(myVector);
}
Run Code Online (Sandbox Code Playgroud)
我一直收到链接器错误,但它编译得很好.关于我做错了什么的任何想法?
更新:基于Iraimbilanja给出的答案,我去重写了代码.但是,万一有人想要使用代码来执行RemoveDuplicates函数,这里是:
//foo.h
Class Foo {
template<typename T>
static void RemoveVectorDuplicates(T& …Run Code Online (Sandbox Code Playgroud) 我试图让我的模板函数产生编译时错误,如果实例化非专用基本版本.我尝试了通常的编译时断言模式(负数组大小),但即使没有实例化模板,编译也会失败.当且仅当基础模板函数被实例化时,有关如何使其失败的任何想法?
template<class Foo> void func(Foo x) {
// I want the compiler to complain only if this function is instantiated.
// Instead, the compiler is complaining here at the declaration.
int Must_Use_Specialization[-1];
}
template<> void func(int x) {
printf("Hi\n");
}
Run Code Online (Sandbox Code Playgroud) 我可以在cpp中定义一个专门的函数,就像这样......
//标题
template<typename T>
void func(T){}
template<>
void func<int>(int);
Run Code Online (Sandbox Code Playgroud)
// cpp
template<>
void func<int>(int)
{}
Run Code Online (Sandbox Code Playgroud)
如何在cpp中的专用类中定义方法?像这样(这不起作用,我得到error C2910: 'A<int>::func' : cannot be explicitly specialized)......
//标题
template<typename T>
struct A
{
static void func(T){}
};
template<>
struct A<int>
{
static void func(int);
};
Run Code Online (Sandbox Code Playgroud)
// cpp
template<>
void A<int>::func(int)
{}
Run Code Online (Sandbox Code Playgroud) 我刚刚在Google C++编码风格指南中注意到了这个项目 - 我并不太明白.
如果我将内联方法或函数放在除其他文件包含的标题之外的文件中,它将不是该类的方法; 它只能用于包含它的代码.那么为什么甚至会有这样的-inl.h文件呢?
另外,为什么我们甚至想要内联长函数呢?(即,除了模板的情况,我们必须将代码放在头文件中进行实例化)
我正在尝试编写一个模板类,可以根据<class>我的传递形成类.问题是我不能在同一个.h文件中声明和定义.在我的项目中,UTF工具只能处理.cpp文件(代码覆盖等).我在博客中看到他们说"添加.cpp而不是.h".这是可取的吗?
Template.h
#ifndef TEMPLATE_H_
#define TEMPLATE_H_
template<class T>
class Template
{
public:
T Add(T a,T b);
};
#endif /* TEMPLATE_H_ */
Run Code Online (Sandbox Code Playgroud)
Template.cpp
#include "Template.h"
template<class T>
T Template<T>::Add(T a, T b)
{
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
Main.cpp
#include "Template.cpp" //Is this a good practise?
#include <iostream>
int main(int argc, char **argv) {
Template<int> obj;
std::cout<<obj.Add(3,4)<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
如果这不可行,那么我该如何解决这个问题呢?export?
我已经搜索并搜索了我的问题的解决方案,但我似乎找不到。我正在使用 Code::Blocks 并且我收到模板类的重新定义错误。
这是我的“vectorAux.h”文件:
#ifndef vectoraux_h
#define vectoraux_h
#include <vector>
#include <algorithm>
#include <iostream>
template <typename T>
void removeDup(std::vector<T> & v);
template <typename T>
unsigned seqVectSearch(const std::vector<T> & v, unsigned first,
unsigned last, const T& target);
template <typename T>
void writeVector(const std::vector<T> & v);
#include "vectorAux.cpp"
#endif
Run Code Online (Sandbox Code Playgroud)
这是我的“vectorAux.cpp”文件:
#include "vectorAux.h"
#include <vector>
#include <algorithm>
#include <iostream>
template <typename T>
void removeDup(std::vector<T> & v)
{
std::vector<int> vector1;
unsigned i, last = v.size();
for(int j = 0; j <= v.size(); j++)
{ …Run Code Online (Sandbox Code Playgroud) 我是C++的新手,但已经在java中获得了一些经验.
在下面的简短c ++练习中,我尝试使用类模板构建堆栈.不幸的是它无法编译,我无法弄清楚原因.
错误消息是:
Stack.cpp:6:错误:预期构造,析构函数,或类型之前转换
‘<’令牌
Stack.cpp:14:错误:前预期初始‘<’令牌
Stack.cpp:25:错误:预期初始化之前‘<’令牌
使[2]:* [构建/Debug/GNU-Linux-x86/Stack.o]错误1
这是Stack.h:
template <class T>
class Stack {
public:
Stack(int = 10);
~Stack() {
delete [] stackPtr;
}
int isEmpty()const {
return top == -1;
}
int isFull() const {
return top == size - 1;
}
int push(const T&);
int pop(T&);
private:
int size; // length i.e. number of elements on Stack.
int top; //index of top element
T* stackPtr;
};
Run Code Online (Sandbox Code Playgroud)
Stack.cpp:
template <class …Run Code Online (Sandbox Code Playgroud)