“如果 constexpr”与非安置新问题

Issue of `if constexpr` with non-placement new

本文关键字:新问题 如果 constexpr      更新时间:2023-10-16

我正在尝试编写一个实用程序,该实用程序根据T是否为聚合类型来调用new T{...}new T(...)。到目前为止,我所达到的如下。请注意,由于此问题,我使用的是宏而不是函数模板。

#define MAKE(p, T, ...) 
  T* p; 
  if constexpr (::std::is_aggregate_v<T>) { 
    p = new T{__VA_ARGS__}; 
  } 
  else { 
    p = new T(__VA_ARGS__); 
  }

我尝试在 gcc 7.2.0 上测试

struct pr_t {
  int a, b;
};
int main() {
  MAKE(p, pr_t, 1, 2);
}

然后发生以下错误(实时(。

prog.cc: In function 'int main()':
prog.cc:9:26: error: new initializer expression list treated as compound expression [-fpermissive]
     p = new T(__VA_ARGS__); 
                          ^
prog.cc:17:3: note: in expansion of macro 'MAKE'
   MAKE(p, pr_t, 1, 2);
   ^~~~
prog.cc:9:26: warning: left operand of comma operator has no effect [-Wunused-value]
     p = new T(__VA_ARGS__); 
                          ^
prog.cc:17:3: note: in expansion of macro 'MAKE'
   MAKE(p, pr_t, 1, 2);
   ^~~~
prog.cc:9:26: error: no matching function for call to 'pr_t::pr_t(int)'
     p = new T(__VA_ARGS__); 
                          ^
prog.cc:17:3: note: in expansion of macro 'MAKE'
   MAKE(p, pr_t, 1, 2);
   ^~~~
prog.cc:12:8: note: candidate: pr_t::pr_t()
 struct pr_t {
        ^~~~
prog.cc:12:8: note:   candidate expects 0 arguments, 1 provided
prog.cc:12:8: note: candidate: constexpr pr_t::pr_t(const pr_t&)
prog.cc:12:8: note:   no known conversion for argument 1 from 'int' to 'const pr_t&'
prog.cc:12:8: note: candidate: constexpr pr_t::pr_t(pr_t&&)
prog.cc:12:8: note:   no known conversion for argument 1 from 'int' to 'pr_t&&'

编译器谈到了p = new T(__VA_ARGS__);的问题。但是,当::std::is_aggregate_v<T>是真的时,不应该根本不考虑吗?

请注意,使用if constexpr和放置 new 的类似模式已经奏效。引自cppref示例。

template<class T, class... Args>
T* construct(T* p, Args&&... args) {
    if constexpr(std::is_aggregate_v<T>) {
        return ::new (static_cast<void*>(p)) T{std::forward<Args>(args)...};
    }
    else {
        return ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);
    }
}

我想非放置版本有什么特别之处?

这与放置与非放置新无关。出现此问题是由于您在宏中扩展__VA_ARGS__而不是在函数中使用完美转发。在这里,编译器仍然尝试编译(扩展后(p = new pr_t(1, 2);即使它永远不会被执行,然后失败。这样做的原因是(正如 Quentin 链接的副本中指出的那样(,由if constexpr产生的丢弃语句仅在封闭模板实例化中没有实例化。作为一个简单的演示,请考虑这也将失败:

#include <iostream>
struct pr_t {
  int a, b;
};
int main() {
  if constexpr (false) {
    pr_t* p = new pr_t(1, 2);
  }
}

如果在函数中使用非放置新功能与放置版本类似,则它有效:

#include <iostream>
#include <memory>
#include <type_traits>
template<class T, class... Args>
std::unique_ptr<T> makeFunc(Args&&... args) {
  if constexpr (std::is_aggregate_v<T>) {
    return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
  }
  else {
    return std::make_unique<T>(std::forward<Args>(args)...);
  }
}
struct Aggregate {
  int a, b;
};
class No_Aggregate {
  int a, b;
public:
  explicit No_Aggregate(int a, int b) : a{a}, b{b} {}
  auto get_a() {
    return a;
  }
  auto get_b() {
    return b;
  }
};
int main() {
  auto agg = makeFunc<Aggregate>(1, 2);
  std::cout << std::is_aggregate_v<Aggregate> << ' '
            << agg->a << ' ' << agg->b << 'n';
  auto n_agg = makeFunc<No_Aggregate>(3, 4);
  std::cout << std::is_aggregate_v<No_Aggregate> << ' ' 
            << n_agg->get_a() << ' ' << n_agg->get_b() << 'n';
}

输出:

1

1 2
0 3 4

完全是因为您尝试在宏中扩展__VA_ARGS__,而不是在模板中std::forward<Args>(args)...

template<class T, class... Args>
T* make(Args&&... args) {
    if constexpr(std::is_aggregate_v<T>) {
        return ::new T{std::forward<Args>(args)...};
    }
    else {
        return ::new T(std::forward<Args>(args)...);
    }
}