考虑到此限制的理由,似乎不太可能改变。
using my_map_type = map<int, int, decltype([] (auto&& lhs, auto&& rhs) {return lhs < rhs*4;})>;
// Assuming the above compiles
my_map_type m( [] (auto&& lhs, auto&& rhs) {return lhs < rhs*4;} );
// Different closure type - compiler error! What do you copy from!?
没有任何合法的方法可以创建第一个lambda的闭包类型的单个对象。因此,即使该规则被删除,你也无法创建my_map_type
的单个实例。其他“将闭包类型作为模板参数”的情况也存在类似的问题。
__builtin_constant_p
的这种特定用法是支持的扩展的一个很好的例子,如果您不介意您的代码只能在GCC和clang上使用,那么可以使用它。至于“如Richard Smith所建议”的部分,对我来说并不清楚它是否表示他推荐了它,还是仅仅是他提出了这个想法。如果他推荐它,那么我没有任何反对任何人用clang的这个技巧的异议。 :) - user743382(*static_cast<closure_type_ptr>(nullptr)) ()
或者 closure_type_ptr()()
,但我不确定是否会使用它,因为它至少处于标准和编译器支持的灰色地带。 - dyp