我刚刚听完了Scott Meyers关于C++ 0x的软件工程电台播客采访.大多数新功能对我来说都很有意义,我现在对C++ 0x感到兴奋,除了一个.我仍然没有得到移动语义 ......它们究竟是什么?
在C++中转换int为等效的最简单方法是什么?string我知道两种方法.有没有更简单的方法?
(1)
int a = 10;
char *intStr = itoa(a);
string str = string(intStr);
Run Code Online (Sandbox Code Playgroud)
(2)
int a = 10;
stringstream ss;
ss << a;
string str = ss.str();
Run Code Online (Sandbox Code Playgroud) 在完美转发中,std::forward用于转换命名的右值引用t1和t2未命名的右值引用.这样做的目的是什么?inner如果我们离开t1&t2作为左值,那将如何影响被调用的函数?
template <typename T1, typename T2>
void outer(T1&& t1, T2&& t2)
{
inner(std::forward<T1>(t1), std::forward<T2>(t2));
}
Run Code Online (Sandbox Code Playgroud) 在这个答案中,我根据类型的is_arithmetic属性定义了一个模板:
template<typename T> enable_if_t<is_arithmetic<T>::value, string> stringify(T t){
return to_string(t);
}
template<typename T> enable_if_t<!is_arithmetic<T>::value, string> stringify(T t){
return static_cast<ostringstream&>(ostringstream() << t).str();
}
Run Code Online (Sandbox Code Playgroud)
dyp建议,不是is_arithmetic类型的属性,是否to_string为类型定义是模板选择标准.这显然是可取的,但我不知道如何说:
如果
std::to_string未定义,则使用ostringstream重载.
声明to_string标准很简单:
template<typename T> decltype(to_string(T{})) stringify(T t){
return to_string(t);
}
Run Code Online (Sandbox Code Playgroud)
这与我无法弄清楚如何构建的标准相反.这显然不起作用,但希望它传达了我正在尝试构建的内容:
template<typename T> enable_if_t<!decltype(to_string(T{})::value, string> (T t){
return static_cast<ostringstream&>(ostringstream() << t).str();
}
Run Code Online (Sandbox Code Playgroud) 我试图选择一种将积分转换为字符串的标准方法,所以我接着通过测量3种方法的执行时间进行了一次小的性能评估
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <chrono>
#include <random>
#include <exception>
#include <type_traits>
#include <boost/lexical_cast.hpp>
using namespace std;
// 1. A way to easily measure elapsed time -------------------
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
template<typename F>
static typename TimeT::rep execution(F const &func)
{
auto start = std::chrono::system_clock::now();
func();
auto duration = std::chrono::duration_cast< TimeT>(
std::chrono::system_clock::now() - start);
return duration.count();
}
};
// -----------------------------------------------------------
// 2. Define the convertion functions …Run Code Online (Sandbox Code Playgroud) C++没有办法获取枚举的字符串表示.人们解决这个问题通过编写又名含有大量的样板代码自定义函数
switch与case XYZ return "XYZ";
那当然要求枚举的用户知道自定义函数的名称.
所以我想我可以添加一个专门化std::to_string来让用户to_string在我的枚举上使用.像这样的东西:
//
#include <iostream>
#include <string>
#include <cassert>
#define TEST
class Car
{
public:
enum class Color
{
Red,
Blue,
White
};
};
#ifdef TEST
#include <string>
namespace std
{
std::string to_string (Car::Color c)
{
switch (c)
{
case Car::Color::Red:
return "Red";
case Car::Color::Blue:
return "Blue";
case Car::Color::White:
return "White";
default:
{
assert(0);
return "";
}
}
}
}
#endif
int main()
{
std::cout << std::to_string(Car::Color::White) << std::endl; …Run Code Online (Sandbox Code Playgroud) 在这个答案中,我创建了一个类型特征:
template<typename T>
using to_string_t = decltype(to_string(declval<T>()));
Run Code Online (Sandbox Code Playgroud)
这工作得很好,但我原本打算使用result_of,现在它让我感到烦恼,我无法弄清楚如何做到这一点.
我正在尝试用以下内容替换上面的行:
template<typename T>
using to_string_t = result_of<to_string(T)>;
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个编译器错误:
错误C2275:'T':非法使用此类型作为表达式
注释:请参阅'T'的声明
错误C2974:'std :: result_of':'_Fty'的模板参数无效,类型预期
我已经尝试了其他几个输入而result_of没有成功,任何人都可以帮助我理解result_of在这里期待什么参数?