llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.01k stars 11.95k forks source link

delete on atomic pointer is ambiguous #40546

Open llvmbot opened 5 years ago

llvmbot commented 5 years ago
Bugzilla Link 41201
Version 8.0
OS Linux
Reporter LLVM Bugzilla Contributor
CC @DougGregor,@zygoloid

Extended Description

#include <atomic>

void test()
{
    delete std::atomic<int*>();
}

Result of compilation with -O3 -std=c++11 is below. gcc does not complain on this code.

<source>:5:5: error: ambiguous conversion of delete expression of type 'std::atomic<int *>' to a pointer
    delete std::atomic<int*>();
    ^      ~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-8.3.0/lib/gcc/x86_64-linux-gnu/8.3.0/../../../../include/c++/8.3.0/atomic:366:7: note: conversion to pointer type 'std::atomic<int *>::__pointer_type' (aka 'int *')
      operator __pointer_type() const noexcept
      ^
/opt/compiler-explorer/gcc-8.3.0/lib/gcc/x86_64-linux-gnu/8.3.0/../../../../include/c++/8.3.0/atomic:369:7: note: conversion to pointer type 'std::atomic<int *>::__pointer_type' (aka 'int *')
      operator __pointer_type() const volatile noexcept
ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 5 years ago

This code is not valid in C++11. The relevant rule is [expr.delete]p1:

The operand shall have a pointer to object type, or a class type having a single non-explicit conversion function ([class.conv.fct]) to a pointer to object type.

This was changed in C++14 to perform a contextual implicit conversion to pointer type; see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3323.pdf for the change in C++14 that made this code valid. (That was voted into the C++14 working draft at the Portland 2012 meeting of the committee.)

Clang accepts this code in C++14 onwards.

It might be reasonable to accept such code as an extension in earlier language modes, especially since all other compilers appear to have applied it retroactively (accepting it even in their strict/pedantic language modes).