vector storing lambdas——CLANG 与其他两个
vector storing lambdas -- CLANG vs the other two
这是代码。
// .
#include <stdio.h>
#include <memory>
#include <vector>
#define CAT_(a, b) a##b
#define CAT(a, b) CAT_(a, b)
#define REG(x) inline auto CAT(unused_name_, __LINE__ ) = tu_register(x)
#define LNE printf("%d", __LINE__ )
typedef void (*fp_required)( ) ;
using fvec_type = std::vector< fp_required > ;
static fvec_type fvec{};
inline bool tu_register( fp_required const & fp ){
printf("\nRegistered: %p" , fp ) ;
fvec.push_back( fp ) ;
return true ;
}
REG([]{ LNE; }) ; // 3 stateless lambdas
REG([]{ LNE; }) ;
REG([]{ LNE; }) ;
int main()
{
printf("\n\nRegistered %lu lambdas. %s\n", fvec.size(), (fvec.size() > 0 ? "Now calling them": ""));
for ( auto & fun : fvec ) {
printf("\nCalling lambda %p , rezult: " , fun);
fun() ;
}
return 0;
}
CLANG 编译时没有警告,但在向量中完全没有放置任何内容?
https://wandbox.org/permlink/nkYjgqvr5QOprKEn
G++ 编译时没有警告,并且按预期工作。
https://wandbox.org/permlink/a6HB6xzavE8FOyOi
也是MSVC(最新的VS2019,全部是最新的),编译运行没问题。
谁是对的?
该代码 应该 有效,在 Clang 8.0.0 中,它确实有效。看起来它在那之后就坏了,在 Wandbox 上的 9.0.0 和 10.0.0 中都不起作用。
这是代码。
// .
#include <stdio.h>
#include <memory>
#include <vector>
#define CAT_(a, b) a##b
#define CAT(a, b) CAT_(a, b)
#define REG(x) inline auto CAT(unused_name_, __LINE__ ) = tu_register(x)
#define LNE printf("%d", __LINE__ )
typedef void (*fp_required)( ) ;
using fvec_type = std::vector< fp_required > ;
static fvec_type fvec{};
inline bool tu_register( fp_required const & fp ){
printf("\nRegistered: %p" , fp ) ;
fvec.push_back( fp ) ;
return true ;
}
REG([]{ LNE; }) ; // 3 stateless lambdas
REG([]{ LNE; }) ;
REG([]{ LNE; }) ;
int main()
{
printf("\n\nRegistered %lu lambdas. %s\n", fvec.size(), (fvec.size() > 0 ? "Now calling them": ""));
for ( auto & fun : fvec ) {
printf("\nCalling lambda %p , rezult: " , fun);
fun() ;
}
return 0;
}
CLANG 编译时没有警告,但在向量中完全没有放置任何内容?
https://wandbox.org/permlink/nkYjgqvr5QOprKEn
G++ 编译时没有警告,并且按预期工作。
https://wandbox.org/permlink/a6HB6xzavE8FOyOi
也是MSVC(最新的VS2019,全部是最新的),编译运行没问题。
谁是对的?
该代码 应该 有效,在 Clang 8.0.0 中,它确实有效。看起来它在那之后就坏了,在 Wandbox 上的 9.0.0 和 10.0.0 中都不起作用。