小编vvv*_*444的帖子

使用 {fmt} 格式化容器

我正在尝试使用 {fmt} 打印 std::vector 或 std::array ,如下所示:

std::vector<double> vec = {1., 2., 3.};
fmt::print("{:0.3f}\n", fmt::join(vec, ","));
Run Code Online (Sandbox Code Playgroud)

问题是,我希望打印一个变换后的向量:

std::vector<double> vec = {1., 2., 3.};
std::vector<double> vec_dup;
std::transform(vec.begin(), vec.end(), std::back_inserter(vec_dup), [](auto x){return x * M_PI;});
fmt::print("{:0.3f}\n", fmt::join(vec_dup, ","));
Run Code Online (Sandbox Code Playgroud)

有没有办法在 C++17 中执行此操作而不需要创建新容器?

c++ c++17 fmt

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

C++ 类型特征来检查类型是否是模板类的实例?

我希望能够推断给定类型是否是模板类型。我查看了 boost 的类型特征类,但找不到与模板相关的 is_* 特征: http://www.boost.org/doc/libs/1_52_0/libs/type_traits/doc/html/index.html

更有趣的是,如果有方法在编译时确定模板参数的属性,例如有多少模板参数或参数是否是模板模板参数。

c++ templates type-traits

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

C++20 中主要比较运算符 (==、&lt;=&gt;) 的反转

据我了解,C++20 标准允许编译器在代码中使用的方向运算符缺失但存在反向运算符时==自动调用反向比较运算符 for 和 。就像这个<=>例子一样:

struct A {
    constexpr A() = default;
};

struct B
{
    constexpr bool operator==(A const& other) const {
        return true;
    }
};

static_assert(B{} == A{}); // This is defined
static_assert(A{} == B{}); // Fails in C++17, automatically generated in C++20
Run Code Online (Sandbox Code Playgroud)

我查看了标准的[class.compare]部分,但我找不到它的说明位置。

有人可以指出标准中的确切位置吗?

c++ language-lawyer c++20

2
推荐指数
1
解决办法
265
查看次数

标准库中外部全局变量的用法

  1. 全局变量通常不被鼓励。C++ 标准库中具有外部链接的全局变量背后的原理是什么?

  2. extern变量真的只是声明而不是定义吗?

std::call_once一个例子是声明具有mutex.h 外部链接的线程局部全局变量的 gcc 实现:

  extern __thread void* __once_callable;
  extern __thread void (*__once_call)();
Run Code Online (Sandbox Code Playgroud)

  /// @cond undocumented
# ifdef _GLIBCXX_HAVE_TLS
  // If TLS is available use thread-local state for the type-erased callable
  // that is being run by std::call_once in the current thread.
  extern __thread void* __once_callable;
  extern __thread void (*__once_call)();

  // RAII type to set up state for pthread_once call.
  struct once_flag::_Prepare_execution
  {
    template<typename _Callable>
      explicit
      _Prepare_execution(_Callable& __c)
      {
    // Store address in thread-local pointer: …
Run Code Online (Sandbox Code Playgroud)

c++ global-variables c++11

2
推荐指数
1
解决办法
334
查看次数