请考虑以下代码:
#include <stdio.h>
namespace Foo {
template <typename T>
void foo(T *, int) { puts("T"); }
template <typename T>
struct foo_fun {
static void fun() { foo((T *)0, 0); };
};
}
namespace Foo {
void foo(int *, int) { puts("int"); }
}
using namespace Foo;
int main() {
foo_fun<int> fun;
fun.fun();
}
Run Code Online (Sandbox Code Playgroud)
什么是预期产量?"T"还是int?
一个编译器(来自Apple的Xcode 3.1.2的gcc 4.0.1)输出"int",另外两个编译器(gcc 4.1.2和4.1.3)输出"T".
如果我在foo(T*,int)版本之前移动foo(int*,int)声明/定义,则全部输出"int".在这种情况下,当前标准是否定义了重载/特化的顺序?
我试图std::basic_string< char, char_traits<char>, allocator<char> >通过<string>标题定义一个完整的特化,其中typedef'd(以g ++为单位).
问题是,如果我<string>首先包含,g ++会将typedef视为实例化basic_string并给我错误.如果我先做专业,那么我没有问题.
我应该能够在<string>包含后定义我的专业化.我该怎么办才能做到这一点?
我的代码:
#include <bits/localefwd.h>
//#include <string> // <- uncommenting this line causes compilation to fail
namespace std {
template<>
class basic_string< char, char_traits<char>, allocator<char> >
{
public:
int blah() { return 42; }
size_t size() { return 0; }
const char *c_str() { return ""; }
void reserve(int) {}
void clear() {}
};
}
#include <string>
#include <iostream>
int main() {
std::cout …Run Code Online (Sandbox Code Playgroud) 我一直试图调用重载的table::scan_index(std::string, ...)成员函数但没有成功.为了清楚起见,我删除了所有不相关的代码.
我有一个被调用的类table,它有一个重载/模板化的成员函数scan_index(),以便将字符串作为特例处理.
class table : boost::noncopyable
{
public:
template <typename T>
void scan_index(T val, std::function<bool (uint recno, T val)> callback) {
// code
}
void scan_index(std::string val, std::function<bool (uint recno, std::string val)> callback) {
// code
}
};
Run Code Online (Sandbox Code Playgroud)
然后有一个hitlist类,它有许多调用的模板化成员函数table::scan_index(T, ...)
class hitlist {
public:
template <typename T>
void eq(uint fieldno, T value) {
table* index_table = db.get_index_table(fieldno);
// code
index_table->scan_index<T>(value, [&](uint recno, T n)->bool {
// code
});
}
}; …Run Code Online (Sandbox Code Playgroud) 以下代码工作正常,一个带有定义和用法的简单模板类
#include <string>
#include <iostream>
using namespace std;
template<class T> class foo{
public:
string what();
};
template<class T> string foo<T>::what(){
return "foo of type T";
}
int main(){
foo<int> f;
cout << f.what() << endl;
}
Run Code Online (Sandbox Code Playgroud)
如果我然后添加以下内容(上面的主要,但在声明模板类foo之后;)
template<> class foo<char>{
public:
string what();
};
template<> string foo<char>::what(){
return "foo of type char";
}
Run Code Online (Sandbox Code Playgroud)
我从g ++收到错误
第19行:错误:模板ID'什么<>'为'std :: string foo :: what()'与任何模板声明都不匹配
这是一个显示错误的键盘:http://codepad.org/4HVBn9oJ
我做了什么明显的错误?或者使用c ++模板是不可能的?是否可以定义所有内联方法(使用模板<> foo的定义)?
再次感谢所有人.
我是C++的新手,我正在尝试使用模板,但我遇到了问题.我要做的是:尝试使用模板计算数字的平方,并且数字可以是基本数据类型,如int,float,以及复数.我还使用模板实现了一个复杂的类,代码如下:
template <typename T>
class Complex {
public:
T real_;
T img_;
Complex(T real, T img) : real_(real), img_(img) { }
};
template <typename T>
T square(T num) {
return num * num;
}
template <>
Complex<typename T> square(Complex<typename T> num) {
T temp_real = num.real_*num.real_ - num.img_*num.img_;
T temp_img = 2 * num.img_ * num.real_;
return Complex(temp_real, temp_img);
}
Run Code Online (Sandbox Code Playgroud)
我试图使用模板专门化来处理特殊情况,但它给了我错误:
using ‘typename’ outside of template
Run Code Online (Sandbox Code Playgroud)
并且错误发生在模板特化方法上.请指出我的错误.谢谢.
我一直在努力理解模板专业化.为什么会产生错误(specialization of 'T foo(T, T) [with T = int]' after instantiation)
template <class T> T foo(T a, T b);
int main()
{
int x=34, y=54;
cout<<foo(x, y);
}
template <class T> T foo(T a, T b)
{
return a+b;
}
template <> int foo<int>(int a, int b)
{
cout<<"int specialization";
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
功能模板的部分特化
我找不到任何解决方案来解决我的问题,因为如果我用我想出的关键词进行搜索会给我一些适合不同问题的解决方案.我明白这之前一定是问过,只是找不到解决办法.
假设我有一个功能模板:
template<class any> print(any value);
Run Code Online (Sandbox Code Playgroud)
我可以像这样专注它让我们说int:
template<> print<int>(int value)
{
std::cout << value;
}
Run Code Online (Sandbox Code Playgroud)
但现在问题是,我希望它也可以使用矢量.由于矢量类是模板类,因此变得困难.
专门化这样的功能:
template<class any> print<vector<any> >(vector<any> value) {}
Run Code Online (Sandbox Code Playgroud)
将生成以下错误(MinGW g ++):
FILE: error: function template partial specialization 'print<vector<any> >' is not allowed
Run Code Online (Sandbox Code Playgroud)
请注意,功能打印只是一个示例.
我怎么解决这个问题?
c++ templates function partial-specialization specialization
我开发了一个简单的模板函数来交换单个字段的字节顺序:
template <typename T> inline void SwapEndian(T& ptr) {
char *bytes = reinterpret_cast<char*>(&ptr);
int a = sizeof(T) / 2;
while (a--) {
char tmp = bytes[a];
int b = sizeof(T) - 1 - a;
bytes[a] = bytes[b];
bytes[b] = tmp;
}
}
Run Code Online (Sandbox Code Playgroud)
我会经常在T = int或float.这两种类型在目标平台上由4个字节表示,并且可以由模板的相同特化处理.
因为这个函数有时负责处理原始数据的大缓冲区,所以我创建了一个优化的特化:
template<> inline void SwapEndian(float& ptr) {
#if defined(__GNUC__)
*reinterpret_cast<unsigned*>(&ptr) = __builtin_bswap32(*reinterpret_cast<unsigned*>(&ptr));
#elif defined(_MSC_VER)
*reinterpret_cast<unsigned*>(&ptr) = __byteswap_ulong(*reinterpret_cast<unsigned*>(&ptr));
#endif
}
Run Code Online (Sandbox Code Playgroud)
这个专门化也适用于32位整数,有符号或无符号,所以我有一大堆重复,只有类型名称不同.
如何通过这个模板路由所有4字节POD类型的实例?(PS.我愿意以不同的方式解决这个问题,但在这种情况下,我想明确知道是否可以构建这些元专用模板.)
编辑:谢谢大家,在阅读了答案并意识到算术比pod更好的限制之后,我受到了启发,写了一些东西.所有答案都很有用,但我只能接受一个,所以我接受了一个似乎在结构上相同的答案.
template<bool, bool> struct SwapEndian_ { template<typename T> static inline void …Run Code Online (Sandbox Code Playgroud) c++ templates endianness specialization template-specialization
我正在寻找模板的帮助.我需要在模板中创建对特定类型有不同反应的函数.
它可能看起来像这样:
template <typename T>
class SMTH
{
void add() {...} // this will be used if specific function isn't implemented
void add<int> {...} // and here is specific code for int
};
Run Code Online (Sandbox Code Playgroud)
我也尝试在单个函数中使用typeid和swich通过类型,但对我不起作用.
在这里,我编写了一个代码片段来查看哪个swap将被调用,但结果都不是.什么都没输出.
#include<iostream>
class Test {};
void swap(const Test&lhs,const Test&rhs)
{
std::cout << "1";
}
namespace std
{
template<>
void swap(const Test&lhs, const Test&rhs)
{
std::cout << "2";
}
/* If I remove the const specifier,then this will be called,but still not the one in global namespace,why?
template<>
void swap(Test&lhs, Test&rhs)
{
std::cout << "2";
}
*/
}
using namespace std;
int main()
{
Test a, b;
swap(a, b);//Nothing outputed
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这swap被称为?而在另一种情况下,为什么swap没有const …
c++ ×10
specialization ×10
templates ×9
overloading ×2
endianness ×1
function ×1
g++ ×1
standards ×1
stl ×1