始终考虑包含我的模板类的以下标头至少包含在两个.CPP文件中,此代码正确编译:
template <class T>
class TClass
{
public:
void doSomething(std::vector<T> * v);
};
template <class T>
void TClass<T>::doSomething(std::vector<T> * v) {
// Do something with a vector of a generic T
}
template <>
inline void TClass<int>::doSomething(std::vector<int> * v) {
// Do something with a vector of int's
}
Run Code Online (Sandbox Code Playgroud)
但请注意专业化方法中的内联.由于该方法被定义多次,因此需要避免链接器错误(在VS2008中为LNK2005).我理解这一点,因为AFAIK完整的模板专业化与简单的方法定义相同.
那么,我该如何删除inline?代码不应该在每次使用时都重复.我搜索了Google,在SO中阅读了一些问题并尝试了许多建议的解决方案但没有成功构建(至少在VS 2008中没有).
谢谢!
我需要为某些类型专门化模板成员函数(比如说double).它工作正常,而类X本身不是模板类,但是当我创建模板时,GCC开始给出编译时错误.
#include <iostream>
#include <cmath>
template <class C> class X
{
public:
template <class T> void get_as();
};
template <class C>
void X<C>::get_as<double>()
{
}
int main()
{
X<int> x;
x.get_as();
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息
source.cpp:11:27: error: template-id
'get_as<double>' in declaration of primary template
source.cpp:11:6: error: prototype for
'void X<C>::get_as()' does not match any in class 'X<C>'
source.cpp:7:35: error: candidate is:
template<class C> template<class T> void X::get_as()
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?这里有什么问题?
提前致谢.
我有一个基类类似于下面的代码.我试图重载<<以与cout一起使用.但是,g ++说:
base.h:24: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Base<T>*)’ declares a non-template function
base.h:24: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
Run Code Online (Sandbox Code Playgroud)
我尝试在类声明/原型中添加<<之后的<>.但是,我明白了does not match any template declaration.我一直试图让操作符定义完全模板化(我想要的),但我只能使用以下代码使用操作符手动实例化.
base.h
template <typename T>
class Base {
public:
friend ostream& operator << (ostream &out, Base<T> *e);
};
Run Code Online (Sandbox Code Playgroud)
base.cpp
ostream& operator<< (ostream &out, Base<int> *e) {
out << e->data;
return …Run Code Online (Sandbox Code Playgroud) 请向我解释为什么以下代码符合并完美运行.我很迷茫.
#include<iostream>
template<class A = int, class B=double>
class Base
{};
template<class B>
class Base <int, B>
{
public:
Base()
{
std::cout<<"it works!!!!!\n";
}
};
int main()
{
Base<> base; // it prints "it works!!!!!"
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它不应该属于模板类Base的通用形式吗?
在C++ 11及更高版本中,是否允许专用std::to_string于std自定义类型的命名空间?
namespace std {
string to_string(::MyClass const & c) { return c.toString(); }
}
Run Code Online (Sandbox Code Playgroud)
示例用例:
int main() {
MyClass c;
std::cout << std::to_string(c) << std::endl;
}
Run Code Online (Sandbox Code Playgroud) 我是模板新手所以也许这是一件微不足道的事情,但我无法让它发挥作用.我试图获得类成员函数的部分特化.最短的代码是:
template <typename T, int nValue> class Object{
private:
T m_t;
Object();
public:
Object(T t): m_t(t) {}
T Get() { return m_t; }
Object& Deform(){
m_t*=nValue;
return *this;
}
};
template <typename T>
Object<T,0>& Object<T,0>::Deform(){
this->m_t = -1;
return *this;
}
int main(){
Object<int,7> nObj(1);
nObj.Deform();
std::cout<<nObj.Get();
}
Run Code Online (Sandbox Code Playgroud)
我试过非成员函数,这很好.什么也工作正常是成员函数的完全专业化.
但是,每当我尝试部分规格时.一个成员函数我得到的形式错误:
PartialSpecification_MemberFu.cpp(17): error: template argument
list must match the parameter list Object<T,0>& Object<T,0>::Deform().
Run Code Online (Sandbox Code Playgroud)
将不胜感激任何帮助:-)
我正在阅读模板完整指南,它说如下:
它在哪里谈论类模板专业化.
但是,如果您专门化类模板,则还必须专门化所有成员函数.虽然可以专门化单个成员函数,但是一旦完成,就不能再专门化整个类了.
我实际上想知道这是怎么回事,因为你可以专门没有任何成员函数.它是说你不能只有一个成员函数的专业化,然后是另一个具有所有成员函数的专业化吗?
有人可以澄清一下吗?
我有以下编译和运行良好的代码:
template<typename T>
T GetGlobal(const char *name);
template<>
int GetGlobal<int>(const char *name);
template<>
double GetGlobal<double>(const char *name);
Run Code Online (Sandbox Code Playgroud)
但是我想删除"默认"功能.也就是说,我想对GetGlobal <t>进行所有调用,其中't'不是int或double错误.
例如,GetGlobal <char>()应该是编译时错误.
我试图删除默认函数,但是,正如我想象的那样,我收到了很多错误.那么有没有办法"禁用"它并允许只调用函数的专用版本?
谢谢!
我有一个像这样的模板化课程:
template<typename T>
class A
{
protected:
std::vector<T> myVector;
public:
/*
constructors + a bunch of member functions here
*/
}
Run Code Online (Sandbox Code Playgroud)
我想只添加一个仅适用于1种给定类型的T的成员函数.是否可以完全执行此操作而无需专门化类并重新实现所有其他已存在的方法?
谢谢
假设我有两个班级:Serializable和Printable.
因此,接受所有派生类的简单模板函数Printable可能如下所示:
template <class T, class B = Printable, class = typename std::enable_if<std::is_base_of<B, T>::value>::type>
void print(T value) {
cout << value << endl;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我希望它也接受所有派生类,Serializable而我仍然可以控制函数体,这显然不起作用:
template <class T, class B = Printable, class = typename std::enable_if<std::is_base_of<B, T>::value>::type>
void print(T value) {
cout << value << endl;
}
template <class T, class B = Serializable, class = typename std::enable_if<std::is_base_of<B, T>::value>::type>
void print(T value) {
cout << value << endl;
}
// Error: Redefinition …Run Code Online (Sandbox Code Playgroud)