This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/70723] New: Missed optimization opportunity for lambda converted to fun-ptr


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70723

            Bug ID: 70723
           Summary: Missed optimization opportunity for lambda converted
                    to fun-ptr
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m.cencora at gmail dot com
  Target Milestone: ---

Hi,

the following code gets properly optimized-out if erasedTypeVTable is
initialized with &dtor<T> (case [2]), but it is not optimized if initialized
with lambda (case [1]).

#include <type_traits>
#include <new>

namespace
{
struct ErasedTypeVTable
{
   using destructor_t = void (*)(void *obj);

   destructor_t dtor;
};

template <typename T>
void dtor(void *obj)
{
   return static_cast<T *>(obj)->~T();
}

template <typename T>
static const ErasedTypeVTable erasedTypeVTable = {
  /* 1 */  [] (void *obj) { return static_cast<T *>(obj)->~T(); }
  /* 2 */ // &dtor<T>
};
struct myType
{
   int a;
};

void meow()
{
   std::aligned_storage<sizeof(myType)>::type storage;
   auto *ptr = new ((char *)(&storage)) myType{5};

   ptr->a = 10;

   erasedTypeVTable<myType>.dtor(ptr);
}

}

int main()
{
   meow();
}

Compiled with -O3 -std=c++14 flags.

g++ --version:
g++ (Ubuntu 6-20160405-0ubuntu1) 6.0.0 20160405 (experimental) [trunk revision
234749]

FWIW, clang 3.8 optimizes both versions.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]