0

我正在尝试编写一个实用程序来调用new T{...}new T(...)基于是否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);
}

然后发生以下错误(live)。

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 constexprwithplacement 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)...);
    }
}

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

4

2 回答 2

3

这与安置与非安置新无关。问题的出现是由于您__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);
  }
}

如果您在函数中使用类似于放置版本的非放置 new ,它可以工作:

#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

于 2018-04-06T08:38:50.530 回答
2

这完全是因为您试图__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)...);
    }
}
于 2018-04-06T08:37:59.227 回答