小编rog*_*mes的帖子

C++程序员应该经常使用std :: flush吗?

是否建议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很多,为了以防万一?或者这些日子在大多数平台上都不再需要了?

c++

21
推荐指数
2
解决办法
1910
查看次数

执行static_assert模板类型是另一个模板

我怎么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)

c++ templates boost c++11

15
推荐指数
1
解决办法
2129
查看次数

在std :: map中复制/移动键/值类型的要求?

这段代码让我困惑:

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键和值类型以及在什么条件下起作用?

c++ c++11

14
推荐指数
1
解决办法
1236
查看次数

为什么不允许double作为非类型模板参数?

在2003年 - 是的,2003年 - 范德沃德和约瑟特在他们的书"C++模板"(第40页)中写道:

无法使用浮点文字(和简单的常量浮点表达式)作为模板参数具有历史原因.因为没有严重的技术挑战,所以在未来的C++版本中可能会支持这一点.

但即使在C++ 11下,这仍然无效:

template<double D> //error
void foo() {}
Run Code Online (Sandbox Code Playgroud)

为什么没有添加?

c++ templates c++11

13
推荐指数
3
解决办法
2136
查看次数

在模板中使用模板别名而不是模板

从上一个问题:

执行static_assert模板类型是另一个模板

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)

c++ templates c++11

11
推荐指数
1
解决办法
2859
查看次数

将其移动到地图后访问一对

如果我将一对移动到地图中,但由于密钥已经存在而插入失败,我可以安全地使用该对吗?

//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)

这是否已在标准中记录?

c++ map move-semantics c++11 std-pair

11
推荐指数
1
解决办法
336
查看次数

boost :: optional的比较(<),输出(<<)和赋值(=)

我有几个关于如何boost::optional工作的问题.我们先来这样做:

boost::optional<int> i;
Run Code Online (Sandbox Code Playgroud)
  1. 是否i < 3总是等同于*i < 3(和其他关系运算符类似)?
  2. 条件是否正确i < 3并且*i < 3未定义?(i还没有设置任何东西)
  3. 什么std::cout << i打印?
  4. 我很确定这i = 3总是一样的*i = 3.如果是这样,我应该选择哪个?

c++ boost boost-optional

6
推荐指数
1
解决办法
1475
查看次数

为什么C++ 11没有包含所有Boost的类型特征?

如你所知,Boost有很多类型特征.C++ 11带来了一些标准,例如is_pointeris_base_of.

还有其他人喜欢has_greaterhas_greater_equalC++ 11没有合并.为什么不?有没有计划将它们纳入下一个标准?标准委员会或Bjarne Stroustrup是否有一些论文?

c++ boost c++11

5
推荐指数
1
解决办法
431
查看次数

比较不同类型的对象是否被认为是好的设计?

你会考虑这个糟糕设计的证据吗?

//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)

例如,如果FooTypedouble测量自纪元秒,BarType是三个整数(年,月,日)在UTC提供的日期,如上述比较"有意义"的元组.

你见过这种类型间的比较吗?他们在C++社区中不受欢迎吗?

c++ operator-overloading

5
推荐指数
1
解决办法
1086
查看次数

对模板中的每个其他类型执行static_assert

如何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)

c++ templates c++11

4
推荐指数
1
解决办法
167
查看次数