是否建议C++程序员经常编写类似的行
std::cout << "output: " << i << " and " << j << std::flush;
//more
std::cout << "ending newline." << std::endl; //endl does flush
Run Code Online (Sandbox Code Playgroud)
换句话说,在没有的输出线中endl,我们应该是不是flush很多,为了以防万一?或者这些日子在大多数平台上都不再需要了?
我怎么static_assert样这个?如果没有C++或C++ 11中的新功能,Boost可能会支持它吗?
template<T>
struct foo {};
template<FooType>
struct bar {
static_assert(FooType is indeed foo<T> for some T,"failure"); //how?
};
Run Code Online (Sandbox Code Playgroud) 这段代码让我困惑:
struct foo {
int i;
foo(int j) : i(j) {}
foo(const foo &) = delete;
foo(foo &&) = delete;
foo &operator=(const foo&) = delete;
foo &operator=(foo&&) = delete;
};
bool operator<(const foo &f1, const foo &f2)
{
return f1.i < f2.i;
}
int main(int argc, char **argv)
{
std::map<foo,int> f;
std::map<foo,int> f2 = f; //error (as expected)
std::map<foo,int> f3 = std::move(f); //no error (why?)
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因为我没有得到任何错误,所以在移动地图时似乎没有创建关键对象(甚至没有将其他关键对象移入其中).
为什么不?我可以根据C++ 11标准依赖这种行为吗?
更一般地说,复制/移动要求std::map对键和值类型以及在什么条件下起作用?
在2003年 - 是的,2003年 - 范德沃德和约瑟特在他们的书"C++模板"(第40页)中写道:
无法使用浮点文字(和简单的常量浮点表达式)作为模板参数具有历史原因.因为没有严重的技术挑战,所以在未来的C++版本中可能会支持这一点.
但即使在C++ 11下,这仍然无效:
template<double D> //error
void foo() {}
Run Code Online (Sandbox Code Playgroud)
为什么没有添加?
从上一个问题:
Andy Prowl为我提供了这个代码,允许我static_assert模板类型是另一种模板类型:
template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of : public std::false_type { };
template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of<TT, TT<Ts...>> : public std::true_type { };
template<typename T>
struct foo {};
template<typename FooType>
struct bar {
static_assert(is_instantiation_of<foo,FooType>::value, ""); //success
};
int main(int,char**)
{
bar<foo<int>> b; //success
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这非常有效.
但是如果我改变这样的代码来使用别名foo,事情会变得糟糕:
template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of : public std::false_type { };
template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of<TT, TT<Ts...>> : public …Run Code Online (Sandbox Code Playgroud) 如果我将一对移动到地图中,但由于密钥已经存在而插入失败,我可以安全地使用该对吗?
//objects available: map, pair
auto insert_pair = map.insert(std::move(pair));
if (!insert_pair.second)
{
//can I safely access pair here?
}
Run Code Online (Sandbox Code Playgroud)
这是否已在标准中记录?
我有几个关于如何boost::optional工作的问题.我们先来这样做:
boost::optional<int> i;
Run Code Online (Sandbox Code Playgroud)
i < 3总是等同于*i < 3(和其他关系运算符类似)?i < 3并且*i < 3未定义?(i还没有设置任何东西)std::cout << i打印?i = 3总是一样的*i = 3.如果是这样,我应该选择哪个?如你所知,Boost有很多类型特征.C++ 11带来了一些标准,例如is_pointer和is_base_of.
还有其他人喜欢has_greater和has_greater_equalC++ 11没有合并.为什么不?有没有计划将它们纳入下一个标准?标准委员会或Bjarne Stroustrup是否有一些论文?
你会考虑这个糟糕设计的证据吗?
//FooType and BarType not in the same hierarchy
bool operator==(const FooType &, const BarType &);
bool operator<(const FooType &, const BarType &);
Run Code Online (Sandbox Code Playgroud)
例如,如果FooType是double测量自纪元秒,BarType是三个整数(年,月,日)在UTC提供的日期,如上述比较"有意义"的元组.
你见过这种类型间的比较吗?他们在C++社区中不受欢迎吗?
如何static_assert对模板中的其他每种类型进行(或其他检查)?
template<typename... Ts> //T1,T2,T3,...
struct foo {
//How can I
//for T1,T3,T5,T7,...
//do some checks, for example:
//static_assert(std::is_default_constructible<Tn>::value,"invalid type");
//static_assert(std::is_copy_constructible<Tn>::value,"invalid type");
};
Run Code Online (Sandbox Code Playgroud)