说我要处理ushort和uint一些方法,但string不同的.所以我猜我需要一个专门的模板string和其他两个ushort和uint.是吗?
// for most
void func(T)(T var) { ... }
// for uint and ushort
void func(T: uint, ushort)(T var) { ... }
Run Code Online (Sandbox Code Playgroud)
这是想法,虽然代码无法编译.它有效还是非常糟糕?
这是家庭作业,虽然已经提交了不同的方法.
我从Visual Studio 2008获得以下内容
error C2893: Failed to specialize function template 'void std::sort(_RanIt,_RanIt,_Pr)'
代码如下
main.cpp
Database<> db;
db.loadDatabase();
db.sortDatabase(sort_by_title());
Database.cpp
void Database<C>::sortDatabase(const sort_by &s) {
std::sort(db_.begin(), db_.end(), s);
}
并且函数对象被定义为
struct sort_by : public std::binary_function<const Media *, const Media *, bool> {
virtual bool operator()(const Media *l, const Media *r) const = 0;
};
struct sort_by_title : public sort_by {
bool operator()(const Media *l, const Media *r) const { ... }
};
...
这里有什么治疗方法?
[编辑]对不起,也许我应该明确继承
template <typename C = std::vector<Media …
c++ inheritance functor specialization template-specialization
我需要(想要)专门化模板类中的方法,只允许某些基本类型.(这不是一个重复的问题,这个)
好吧,我有这个课程:
template<typename TYPE, size_t NB>
class X
{
public:
template<typename arg_type>
X& get(const arg_type var);
}
Run Code Online (Sandbox Code Playgroud)
我想专门化arg_type只允许无符号整数,如下所示:
template<typename TYPE, size_t NB> template<unsigned long> X& X::get(const unsigned long val);
Run Code Online (Sandbox Code Playgroud)
但可以肯定的是,上述内容在msvc2011和gcc上都不起作用
更具体地说,我尝试做的是基于上面的模板化类型编写代码,并编写专门化,以便使用此类X的任何人都不能使用此方法而不是我专门的方法.
这甚至可能吗?如果是的话,这样做是不是很糟糕?
在此先感谢,jav974
我正在寻找良好的做法,以避免一遍又一遍地重写相同的代码,以实现unboxedness.说我有这样的事情:
def speedyArrayMaker[@specialized(Long) A: ClassTag](...): Array[A] = {
val builder = Array.newBuilder[A]
// do stuff with builder
builder.result
}
Run Code Online (Sandbox Code Playgroud)
这将导致在我builder可能的情况下存在未装箱的存储,但据我所知,没有任何未装箱的方法调用它,因为我正在经历非特殊化的ArrayBuilder特征.
在一个专门的单形世界中Long,我会写val builder = new ArrayBuilder.ofLong()和避免拳击,但缺乏说明ArrayBuilder/ Builder专注于所有原始类型,我想不出一个避免重复努力的好方法.我想到的一种方法可能是speedyArrayMaker:
val (add, builder): (A => Unit, ArrayBuilder[A]) = implicitly[ClassTag[A]].runtimeClass match {
case java.lang.Long.TYPE =>
val builder = new ArrayBuilder.ofLong()
((x: Long) => builder += x, builder).asInstanceOf
case _ =>
val builder = Array.newBuilder[A]
((x: A) => builder += x, builder)
} …Run Code Online (Sandbox Code Playgroud) 我有以下课程:
class Foo {
public:
template <typename T>
T bar() {
cout << "Called with return type: " << typeid(T).name() << endl;
T t = //... (some implementation here)
return t;
}
}
Run Code Online (Sandbox Code Playgroud)
它以下列方式调用:
Foo foo;
int i = foo.bar<int>();
long l = foo.bar<long>();
Run Code Online (Sandbox Code Playgroud)
现在,我想对调用函数的情况有不同的专业化 shared_ptr<T>
Foo foo;
foo.bar<shared_ptr<int>>();
foo.bar<shared_ptr<long>>();
Run Code Online (Sandbox Code Playgroud)
但是我当然不想为每种类型创建完整的专业化.是否有可能实现这种行为(如果需要可以基于特质)?
我从gcc 4.8.3和clang 3.2获得了一致的行为,但不明白它为什么会发生.尽管我有一个类模板的显式实例化,但代码没有生成,当我使用模板的完全专用的实例时,我得到一个未定义的符号.
我在文件'temp.hpp'中有一个简单的类模板定义
#pragma once
template <typename T1>
class C
{
public:
C (T1 c) : d_c(c) {};
~C () = default;
void print ();
private:
T1 d_c;
};
Run Code Online (Sandbox Code Playgroud)
请注意,方法'print()'已声明,但未在此处定义.我想要.cpp文件中的定义,它将专门用于不同类型.
所以在temp.cpp文件中我有print()方法的默认定义
#include "temp.hpp"
#include <iostream>
template<typename T1>
void
C<T1>::print ()
{
std::cout << "Printing: " << d_c << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
然后是类型'float'的类的特化:
template <>
class C <float>
{
public:
C (float f) : d_f(f) {};
~C () = default;
void print ()
{
std::cout << "float: " << d_f << …Run Code Online (Sandbox Code Playgroud) 编辑:当我去度假时,我实际上没有机会测试任何建议的解决方案,当我回来时,负责类模板的人做了一些改变,让我可以绕过需要使用类模板本身定义的类型.
感谢大家的帮助.
简而言之 - 并且随意纠正我的措辞,模板对我来说仍然有点巫术, - 我需要知道我是否可以在我的专业类中使用(protected)struct或#typedef定义的类模板.例如:
这是类模板:
template<typename T>
class A : public C<T>
{
protected:
struct a_struct { /* Class template implementation, doesn't depend on T */ };
void foo( a_struct a );
};
Run Code Online (Sandbox Code Playgroud)
我需要完全专注于T = VAL:
template<>
class A< VAL > : public C< VAL >
{
void foo( a_struct a )
{
// My implementation of foo, different from the class template's
}
};
Run Code Online (Sandbox Code Playgroud)
但是,如果我做这样的事情,编译器抱怨a_struct在我的专门课程中未定义.我尝试了从类模板中专门化和继承,但是......太乱了. …
我的代码遵循以下一般设计:
protocol DispatchType {}
class DispatchType1: DispatchType {}
class DispatchType2: DispatchType {}
func doBar<D:DispatchType>(value:D) {
print("general function called")
}
func doBar(value:DispatchType1) {
print("DispatchType1 called")
}
func doBar(value:DispatchType2) {
print("DispatchType2 called")
}
Run Code Online (Sandbox Code Playgroud)
其中,在现实中DispatchType其实是一个后端存储.这些doBar函数是依赖于正确存储类型的优化方法.如果我这样做,一切正常:
let d1 = DispatchType1()
let d2 = DispatchType2()
doBar(value: d1) // "DispatchType1 called"
doBar(value: d2) // "DispatchType2 called"
Run Code Online (Sandbox Code Playgroud)
但是,如果我创建一个调用的函数doBar:
func test<D:DispatchType>(value:D) {
doBar(value: value)
}
Run Code Online (Sandbox Code Playgroud)
我尝试了类似的呼叫模式,我得到:
test(value: d1) // "general function called"
test(value: d2) // "general function called"
Run Code Online (Sandbox Code Playgroud)
这似乎是Swift应该能够处理的东西,因为它应该能够在编译时确定类型约束.就像快速测试一样,我也试着写作doBar:
func …Run Code Online (Sandbox Code Playgroud) 所以这个例子来自:http://en.cppreference.com/w/cpp/utility/variant/visit声明了专门的类型:
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
Run Code Online (Sandbox Code Playgroud)
这里构造为r值:
std::visit(overloaded {
[](auto arg) { std::cout << arg << ' '; },
[](double arg) { std::cout << std::fixed << arg << ' '; },
[](const std::string& arg) { std::cout << std::quoted(arg) << ' '; },
}, v);
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚它是如何工作的.overloaded从这里继承的类型是什么?它看起来像是一群lambdas,但我不知道它会如何operator().有人可以解释继承在这里是如何工作的吗?
c++ operators specialization template-inheritance variadic-templates
考虑
using namespace std;
template <typename S, typename T> struct hash<pair<S, T>>
{
inline size_t operator()(const pair<S, T> &v) const
{
return 0;
}
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,GCC和Clang都可以编译好,没有任何警告.然而,这似乎与我在线阅读的内容相矛盾,即定义您自己的哈希函数与标准库的无序类型一起使用需要您将定义放在std命名空间中.
有趣的是,专门针对pair<int, int>:
template <> struct hash<pair<int, int>>
{
size_t operator()(const pair<int, int> &v) const
{
size_t seed = 0;
return seed;
}
};
Run Code Online (Sandbox Code Playgroud)
导致我们预期的错误.
但是,为什么第一个不会导致任何编译器警告,尽管我们没有将它放在std命名空间中?
specialization ×10
c++ ×7
templates ×5
c++11 ×2
boxing ×1
d ×1
dispatch ×1
functor ×1
generics ×1
inheritance ×1
jvm ×1
operators ×1
overriding ×1
scala ×1
swift ×1