Lambda-expression作为常量表达式的未求值子表达式
Lambda-expression as an unevaluated sub-expressions of a constant expression
请原谅我的长帖子,但我不能让这个程序工作,除非我指定-fpermissive
到gcc,而不是在clang下。你能帮忙修复这个例子吗?
namespace detail
{
template<typename T>
constexpr auto address(T&& t) ->
typename ::std::remove_reference<T>::type*
{
return &t;
}
template <typename FP, FP fp, class C, typename ...A>
struct S
{
static constexpr auto* l = false ? address(
[](C* const object) noexcept
{
return [object](A&& ...args) {
return (object->*fp)(::std::forward<A>(args)...);
};
}) :
nullptr
;
};
template <typename FP, FP fp, typename R, class C, typename ...A>
auto make_member_delegate(C* const object, R (C::* const)(A...)) ->
decltype((*S<FP, fp, C, A...>::l)(object))
{
return (*S<FP, fp, C, A...>::l)(object);
}
}
template <typename FP, FP fp, class C>
auto make_member_delegate(C* const object) ->
decltype(detail::make_member_delegate<FP, fp>(object, fp))
{
return detail::make_member_delegate<FP, fp>(object, fp);
}
struct A
{
void hello()
{
::std::cout << "it worked" << ::std::endl;
}
};
int main()
{
A a;
auto d(make_member_delegate<decltype(&A::hello), &A::hello>(&a));
d();
return 0;
}
错误是(首先是gcc,然后是clang++):
gcc-4.9.0:
t.cpp:20:26: error: 'constexpr detail::S<void (A::*)(), &A::hello, A>::<lambda(A*)>* const detail::S<void (A::*)(), &A::hello, A>::l', declared using local type 'detail::S<void (A::*)(), &A::hello, A>::<lambda(A*)>', is used but never defined [-fpermissive]
static constexpr auto* l = false ? address(
叮当声+ + 3.4.2:
t.cpp:21:5: error: a lambda expression may not appear inside of a constant expression
[](C* const object) noexcept
^
t.cpp:33:14: note: in instantiation of template class 'detail::S<void (A::*)(), &A::hello, A>' requested here
decltype((*S<FP, fp, C, A...>::l)(object))
^
t.cpp:32:6: note: while substituting deduced template arguments into function template 'make_member_delegate' [with FP = void
(A::*)(), fp = &A::hello, R = void, C = A, A = <>]
auto make_member_delegate(C* const object, R (C::* const)(A...)) ->
^
t.cpp:41:6: note: while substituting deduced template arguments into function template 'make_member_delegate' [with FP = void
(A::*)(), fp = &A::hello, C = A]
auto make_member_delegate(C* const object) ->
奇怪的是,规范的python示例在clang-3.4.2
这是gcc-4.9.0
的修复,但程序仍然无法与clang-3.4.2
编译:
template <typename FP, FP fp, typename R, class C, typename ...A>
auto make_delegate(C* const object, R (C::* const)(A...)) ->
decltype((*decltype(S<FP, fp, C, A...>::l)(nullptr))(object))
{
return (*decltype(S<FP, fp, C, A...>::l)(nullptr))(object);
}
相关文章:
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 使用自动推导的 lambda 参数作为常量表达式
- 生成提升::hana::set 的常量表达式问题
- 为什么不能用常量表达式声明数组?
- 不是 lambda 函数中的常量表达式
- 函数调用在常量表达式中必须具有常量值
- 错误:constexpr 变量'struct2Var'必须由常量表达式初始化
- 如何在常量计算表达式中获取编译时错误?
- 关于在需要常量表达式的上下文中使用的glvalue常量表达式的问题
- 生成 constexpr 字符串表,不能产生常量表达式
- 整体模板参数。错误:在常量表达式中使用'this'
- 如何在满足常量表达式的同时将整数传递给指针,传递给 std::array<double、integer>?
- 编译器错误:函数调用在常量表达式中必须有一个常量值
- 错误:'new'不能出现在常量表达式中
- 我可以写出小于 -0.5 两个 ulps 的常量表达式双精度吗?
- 编译器在传递 const 变量时返回错误:模板参数不是常量表达式
- 为什么我不能在非常量表达式上使用此模板阶乘函数?
- C++ 使用变量而不是常量表达式初始化数组