假设我有两个班级: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) 是否可以专门化模板类的特定成员?就像是:
template <typename T,bool B>
struct X
{
void Specialized();
};
template <typename T>
void X<T,true>::Specialized()
{
...
}
template <typename T>
void X<T,false>::Specialized()
{
...
}
Run Code Online (Sandbox Code Playgroud)
当然,这段代码无效.
专业化有望为原始类型提供高效率的实现,只需极少的额外样板.但专业化似乎过于渴望自己的利益.如果我想专门化一个类或方法,
def foo[@specialized(Byte) A](a: A): String = ???
class Bar[@specialized(Int) B] {
var b: B = ???
def baz: B = ???
}
Run Code Online (Sandbox Code Playgroud)
那么我需要编写一个涵盖专用和通用案例的实现.如果这些情况彼此真的不同,那么实现不会重叠怎么办?例如,如果我想在字节上执行数学运算,我需要& 0xFF在逻辑中插入一堆s.
我可以编写一个专门的类型类来正确地进行数学运算,但是这不会只是将同一个问题推回一个级别吗?如何+以不与更一般的实现冲突的方式为该类型类编写专用方法?
class Adder[@specialized(Byte) A] {
def +(a1: A, a2: A): A = ???
}
Run Code Online (Sandbox Code Playgroud)
此外,一旦我以这种方式创建一个类类,我如何确保正确的类型类用于我的专用方法而不是通用版本(如果它是真正的通用,应该可能编译,当然会运行,除了它不是我想要的)?
有没有办法在没有宏的情况下做到这一点?使用宏更容易吗?
我想让这个专业的w/o改变主要.是否可以基于其基类来专门化某些东西?希望如此.
-编辑-
我将有几个继承自SomeTag的类.我不想为每个人写相同的专业.
class SomeTag {};
class InheritSomeTag : public SomeTag {};
template <class T, class Tag=T>
struct MyClass
{
};
template <class T>
struct MyClass<T, SomeTag>
{
typedef int isSpecialized;
};
int main()
{
MyClass<SomeTag>::isSpecialized test1; //ok
MyClass<InheritSomeTag>::isSpecialized test2; //how do i make this specialized w/o changing main()
return 0;
}
Run Code Online (Sandbox Code Playgroud) 鉴于:
template<typename T>
inline bool f( T n ) {
return n >= 0 && n <= 100;
}
Run Code Online (Sandbox Code Playgroud)
与unsigned类型一起使用时会生成警告:
unsigned n;
f( n ); // warning: comparison n >= 0 is always true
Run Code Online (Sandbox Code Playgroud)
有没有什么聪明的办法不是做比较n >= 0时T是一个unsigned类型?我尝试添加部分模板专门化:
template<typename T>
inline bool f( unsigned T n ) {
return n <= 100;
}
Run Code Online (Sandbox Code Playgroud)
但是gcc 4.2.1并不喜欢这样.(我没想到的是一种模板偏特的将是法律反正.)
This is probably only a syntax problem.
So i have this template class :
template <typename String, template<class> class Allocator>
class basic_data_object
{
template<typename T>
using array_container = std::vector<T, Allocator<T>>;
};
Run Code Online (Sandbox Code Playgroud)
And another one :
template <typename String, template<class> class Allocator, typename T>
struct get_data_object_value
{
};
Run Code Online (Sandbox Code Playgroud)
Now i want to specialize the second one's T parameter with the first one's inner typedef array_container for any given type.
template <typename String, template<class> class Allocator, typename T>
struct get_data_object_value
<String, Allocator, …Run Code Online (Sandbox Code Playgroud) class A
{
};
template <typename A, int S>
class B
{
public:
static int a[S];
B()
{
a[0] = 0;
}
};
template<> int B<A, 1>::a[1];
int main()
{
B<A, 1> t;
t;
}
Run Code Online (Sandbox Code Playgroud)
它在GCC 4.1下编译,但没有链接:
static.cpp:(.text._ZN1BI1ALi1EEC1Ev[B<A, 1>::B()]+0x5): undefined reference to `B<A, 1>::a'
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我宁愿保持初始化专用,因为数组包含特定于该类型的一些数据.
我需要在c ++中专门化一个函数模板.
template<typename T>
void doStuff<T>() {}
Run Code Online (Sandbox Code Playgroud)
至
template<>
void doStuff<DefinedClass>();
Run Code Online (Sandbox Code Playgroud)
和
template<>
void doStuff<DefinedClass2>();
Run Code Online (Sandbox Code Playgroud)
我猜这不是正确的语法(因为它没有编译).我该怎么办?
另外,由于我没有未定义的模板参数doStuff<DefinedClass>,是否可以在.cpp中声明主体?
注意:doStuff将使用T wihtin其主体来声明变量.
我试图根据类模板参数确定调用哪个版本的成员函数.我试过这个:
#include <iostream>
#include <type_traits>
template<typename T>
struct Point
{
void MyFunction(typename std::enable_if<std::is_same<T, int>::value, T >::type* = 0)
{
std::cout << "T is int." << std::endl;
}
void MyFunction(typename std::enable_if<!std::is_same<T, int>::value, float >::type* = 0)
{
std::cout << "T is not int." << std::endl;
}
};
int main()
{
Point<int> intPoint;
intPoint.MyFunction();
Point<float> floatPoint;
floatPoint.MyFunction();
}
Run Code Online (Sandbox Code Playgroud)
我认为这是说"如果T是int,则使用第一个MyFunction,如果T不是int,则使用第二个MyFunction,但我得到编译器错误"错误:'struct std :: enable_if'中没有类型名为'type'谁能指出我在这里做错了什么?
我有一个自动指针类,在构造函数中我传入一个指针.我希望能够从新的[]在构造函数中分离出来的新,这样我可以正确地调用删除或析构函数删除[].这可以通过模板专业化来完成吗?我不想在构造函数中传入一个布尔值.
template <typename T>
class MyAutoPtr
{
public:
MyAutoPtr(T* aPtr);
};
// in use:
MyAutoPtr<int> ptr(new int);
MyAutoPtr<int> ptr2(new int[10]);
Run Code Online (Sandbox Code Playgroud)