jeudi 3 novembre 2016

Can sizeof be applied inside a lambda on a variable that is not captured or is this a compiler bug?

This is a follow up of the discussion found here.

The following code compiles both under gcc and clang (live demo). This is surprising for the case in line //1 since the lambda does not capture anything. For the case of MCR2, where the lambda returns the pointer itself, we get the expected compile time error (line // Will not compile). How is application of operator sizeof different from returning the pointer?

#include <iostream>

#define MCR1(s) \
  ([]() { return sizeof(s); })()

#define MCR2(s) \
  ([]() { return s; })()

int main() {
  auto *s= "hello world";

  auto x1 = MCR1( s ); //1
  auto y1 = MCR1( "hello world" );
//  auto x2= MCR2( s ); // Will not compile
  auto y2= MCR2( "hello world" );

  std::cout << x1  << "  " << y1  << '\n';
  std::cout // << x2 << "  " 
            << y2 << '\n';
}

Aucun commentaire:

Enregistrer un commentaire