使用带有该指针

时间:2015-12-31 11:36:53

标签: c++ reflection lambda c++14

我有3个编译器不同意下面的C ++ 14代码:

#include <type_traits>

template <typename P, typename F>
constexpr auto can_call(P* p, F&& f) -> decltype(f(p),std::true_type{}) { return {}; }

template <typename F>
constexpr std::false_type can_call(void const*, F&&) { return {}; }

struct C {
  C() { auto pc = can_call(this, [&](auto p) -> decltype(p->f(3)) {}); }
  void f(int);
};

template <typename T>
struct CT {
  CT() { auto ptc = can_call(this, [&](auto p) -> decltype(p->f(3)) {}); }
  void f(T);
};

int main()
{
  C oc;
  auto pc = can_call(&oc, [&](auto p) -> decltype(p->f(3)) {});
  CT<int> oct;
  auto ptc = can_call(&oct, [&](auto p) -> decltype(p->f(3)) {});
}

clang ++ 3.7毫不犹豫地接受了它。

g ++ 4.9,5.1,5.2,5.3和trunk使CT<T>构造函数失败,并显示以下消息:

t.cpp: In instantiation of ‘CT<T>::CT() [with T = int]’:
t.cpp:24:11:   required from here
t.cpp:16:61: error: base operand of ‘->’ has non-pointer type ‘auto:2’
     CT() { auto ptc = can_call(this, [&](auto p) -> decltype(p->f(3)) {}); }
                                                              ^

VisualStudio 2015社区与g ++完全相反,并且除了模板CT<T>构造函数之外,所有lambdas都失败了,抱怨'->f'的左边必须是指针。 (我没有检查是否错误地在std::false_type构造函数中获得CT<T>结果。)

由此我有两个问题:

  1. 期待什么?我曾尝试从github阅读(草案C ++ 17)标准,但说实话,我甚至不知道在哪里看,特别是因为编译器有点不同意。
  2. 预期的反射能否以不同的方式实现?我希望(不需要)该功能来改进库中的编译错误消息。

0 个答案:

没有答案