Trying to answer this question using the "const block" on the basis of lambda, I was faced with a situation where clang++, and g++do not agree with the code period.
#include <iostream>
#include <type_traits>
struct noncopyable
{
int a;
const noncopyable& operator=(noncopyable&&) = delete;
noncopyable(noncopyable&&) = delete;
const noncopyable& operator=(const noncopyable&) = delete;
noncopyable(const noncopyable&) = delete;
~noncopyable() = default;
};
void modify(noncopyable& a)
{
a.a = 0;
}
int main()
{
noncopyable a = { 25 }, b = { 42 };
[&, &a = static_cast<const noncopyable&>(a)]{
modify(b);
}();
std::cout << a.a << " " << b.a << "\n";
}
clang++receives the code and displays the expected output, and g++fails with the following error message:
a.cpp: In function ‘int main()’:
a.cpp:22:5: error: binding ‘const noncopyable’ to reference of type ‘noncopyable&’ discards qualifiers
[&, &a = static_cast<const noncopyable&>(a)]{
^
clang++ -v is an
clang version 3.7.0 (tags/RELEASE_370/final)
Target: x86_64-redhat-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/5.1.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/5.1.1
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/5.1.1
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
and g++ -vhave
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/5.1.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --disable-libgcj --with-isl --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 5.1.1 20150618 (Red Hat 5.1.1-4) (GCC)
Parameters used to compile the code:
-std=c++14 -Wall -Wextra -pedantic
Which compiler is right? g++or clang++? (or, third option, is this code invalid for another reason?)