我想专门为vector和map映射容器的函数模板.对于矢量我可以像下面这样做,但我不知道我怎么能有一个专门版本的功能,只用于地图像容器.
#include <iostream>
#include <vector>
#include <map>
using namespace std;
template<typename Iterator>
void print(Iterator begin, Iterator end)
{
while (begin != end)
{
cout << *begin << endl; // compiler error for map like containers
++begin;
}
}
int main()
{
vector<int> noVec = { 1, 2, 3 };
print(noVec.begin(), noVec.end());
map<int, int> nosMap;
nosMap[0] = 1;
nosMap[1] = 2;
nosMap[3] = 3;
print(nosMap.begin(), nosMap.end());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个问题是类似的,但它建议在向量中使用对,我不想这样做.我知道专业化可以通过SFINAE完成,但不知道要检查的条件.如果我能用C++ 11 type_traits实现这一目标会很棒.
基本上我想在C ++中模拟.NET Exception.InnerException。我想从底层捕获异常,并用另一个异常包装它,然后再次抛出到上层。这里的问题是我不知道如何将捕获的异常包装在另一个异常中。
struct base_exception : public std::exception
{
std::exception& InnerException;
base_exception() : InnerException(???) { } // <---- what to initialize with
base_exception(std::exception& innerException) : InnerException(innerException) { }
};
struct func1_exception : public base_exception
{
const char* what() const throw()
{
return "func1 exception";
}
};
struct func2_exception : public base_exception
{
const char* what() const throw()
{
return "func2 exception";
}
};
void func2()
{
throw func2_exception();
}
void func1()
{
try
{
func2();
}
catch(std::exception& e)
{
throw …
Run Code Online (Sandbox Code Playgroud) 我遇到过这段代码
int n1 = 10;
int n2 = (int &)n1;
Run Code Online (Sandbox Code Playgroud)
我不明白这个转换的含义,n2不是引用,因为修改n1不反映n1.奇怪的是,这段代码在gcc中抛出编译器错误,因为在VC++中编译很好.
有人知道这个演员的意思吗?
Windows Portable Executables是否真的可以跨机器架构移植?如果是这样,它如何运作?如果不是那么"可移植可执行文件"是什么意思或可执行文件的哪个部分是可移植的?
谢谢,Siva Chandran