User defined operator conversion resolution order - c++

Is it possible to configure the resolution order of user defined operators? Consider the following code:
#include <memory>
#include <utility>
using P = std::unique_ptr<int>;
struct S{
P p;
operator P() && { return std::move(p); }
operator const P&() const { return p; }
};
S s{std::make_unique<int>()};
P p;
p = std::move(s);
This fails to compile:
In file included from /opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/memory:76,
from <source>:1:
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/unique_ptr.h:406:19: note: candidate: 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = int; _Dp = std::default_delete<int>]'
406 | unique_ptr& operator=(unique_ptr&&) = default;
| ^~~~~~~~
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/bits/unique_ptr.h:515:19: note: candidate: 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]' (deleted)
515 | unique_ptr& operator=(const unique_ptr&) = delete;
It seems compilation is failing because of an ambiguity between the deleted copy assignment and defaulted move assignment operators.
Why is the compiler unable to resolve the operation here? Is there a way to define the operators to make this work?

If these operators are supposed to be conversion operators, you can define them like this:
operator P() && { return std::move(p); }
operator const P&() & { return p; }
Demo
The second operator needs the lvalue ref-qualifier (the & at the end) because without it, the implicit object parameter can also be an rvalue. That case is also covered with the rvalue ref-qualified version, so the call becomes ambiguous. Same thing if you add the const qualifier, because an rvalue can bind to a const lvalue reference.

You didn't define conversion/cast operators, you defined call operators. As written, you'd need to do:
p = std::move(s)();
to call s.
If you want a user-defined conversion, you'd want to change:
P operator() && { return std::move(p); }
const P& operator() const { return p; }
to something like:
operator P() && { return std::move(p); }
operator P() const { return p; }
though I'm not sure whether that paired overload of a conversion operator is legal.

Related

why does ranges::view_interface<T>::size require a move constructor

I don't understand where the requirement for moving comes from. I can't find it in forward_range and sized_sentinel...
Basic example:
#include <ranges>
#include <string>
#include <iostream>
class vrange: public std::ranges::view_interface<vrange>
{
public:
vrange(std::string &d): data(d){;};
vrange(const vrange &&) = delete;
auto begin() const noexcept { return data.begin(); };
auto end() const noexcept { return data.end(); };
private:
std::string data;
};
int main(){
std::string h("Hello world");
vrange r(h);
std::cout << r.size() << std::endl;
for (const auto &i: r){
std::cout << i;
}
std::cout << std::endl;
}
removing the call to r.size(), or defaulting the vrange move constructor and assignment operator makes it compile fine.
compiler message:
/usr/lib/gcc/x86_64-pc-linux-gnu/12/include/g++-v12/bits/ranges_util.h: In instantiation of ‘constexpr _Derived& std::ranges::view_interface<_Derived>::_M_derived() [with _Derived = vrange]’:
/usr/lib/gcc/x86_64-pc-linux-gnu/12/include/g++-v12/bits/ranges_util.h:101:35: required from ‘constexpr bool std::ranges::view_interface<_Derived>::empty() requires forward_range<_Derived> [with _Derived = vrange]’
w.cpp:25:12: required from here
/usr/lib/gcc/x86_64-pc-linux-gnu/12/include/g++-v12/bits/ranges_util.h:70:23: error: static assertion failed
70 | static_assert(view<_Derived>);
| ^~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/12/include/g++-v12/bits/ranges_util.h:70:23: note: constraints not satisfied
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/12/include/g++-v12/ranges:37:
/usr/lib/gcc/x86_64-pc-linux-gnu/12/include/g++-v12/concepts:136:13: required for the satisfaction of ‘constructible_from<_Tp, _Tp>’ [with _Tp = vrange]
/usr/lib/gcc/x86_64-pc-linux-gnu/12/include/g++-v12/concepts:150:13: required for the satisfaction of ‘move_constructible<_Tp>’ [with _Tp = vrange]
/usr/lib/gcc/x86_64-pc-linux-gnu/12/include/g++-v12/concepts:247:13: required for the satisfaction of ‘movable<_Tp>’ [with _Tp = vrange]
/usr/lib/gcc/x86_64-pc-linux-gnu/12/include/g++-v12/concepts:137:30: note: the expression ‘is_constructible_v<_Tp, _Args ...> [with _Tp = vrange; _Args = {vrange}]’ evaluated to ‘false’
137 | = destructible<_Tp> && is_constructible_v<_Tp, _Args...>;
This has nothing to do with size specifically.
view_interface is used to build a type that is a view. Well, the ranges::view concept requires that the type is at least moveable. And view_interface has a very specific requirement on the type given as its template argument:
Before any member of the resulting specialization of view_interface other than special member functions is referenced, D shall be complete, and model both derived_from<view_interface<D>> and view.
Well, your type does not model view because it is not moveable. So you broke the rules, and you therefore get undefined behavior. Which can include compile errors happening if you call certain members but not others.

User-defined conversion operator doesn't work on references

I have a simple primitive type wrapper:
template <typename T>
class Scalar {
public:
explicit Scalar(T value) : value{value} {}
Scalar(Scalar&& other) = default;
Scalar& operator=(Scalar&& other) = default;
Scalar(const Scalar& other) = default;
Scalar& operator=(const Scalar& other) = default;
template <typename U>
explicit operator Scalar<U>() {
return Scalar<U>{static_cast<U>(this->value)};
}
inline T getValue() const noexcept { return this->value; }
private:
T value;
};
Casting Scalar values works well, but somehow it fails for references, e.g.
auto a = Scalar<double>{2.54};
Scalar<int> b = static_cast<Scalar<int>>(a); // works
const auto& c = a;
Scalar<int> d = static_cast<Scalar<int>>(c); // fails
Here's the compilation error (can be checked here http://rextester.com/GOPYU13091), any ideas what might be the issue here?
source_file.cpp: In function ‘int main()’:
source_file.cpp:31:47: error: no matching function for call to ‘Scalar(const Scalar<double>&)’
Scalar<int> d = static_cast<Scalar<int>>(c);
^
source_file.cpp:12:3: note: candidate: Scalar<T>::Scalar(const Scalar<T>&) [with T = int] <near match>
Scalar(const Scalar& other) = default;
^
source_file.cpp:12:3: note: conversion of argument 1 would be ill-formed:
source_file.cpp:31:47: error: could not convert ‘c’ from ‘const Scalar<double>’ to ‘const Scalar<int>&’
Scalar<int> d = static_cast<Scalar<int>>(c);
^
source_file.cpp:10:3: note: candidate: Scalar<T>::Scalar(Scalar<T>&&) [with T = int] <near match>
Scalar(Scalar&& other) = default;
^
source_file.cpp:10:3: note: conversion of argument 1 would be ill-formed:
source_file.cpp:31:47: error: could not convert ‘c’ from ‘const Scalar<double>’ to ‘Scalar<int>&&’
Scalar<int> d = static_cast<Scalar<int>>(c);
^
This is a const vs non-const issue, not a reference vs object issue.
Using
auto& c = a;
Scalar<int> d = static_cast<Scalar<int>>(c);
works for me.
By changing the user defined conversion operator to const member function
template <typename U>
explicit operator Scalar<U>() const {
return Scalar<U>{static_cast<U>(this->value)};
}
also makes sure that the following works.
const auto& c = a;
Scalar<int> d = static_cast<Scalar<int>>(c);

boost optional and user-defined conversion

I am unable to write a correct user defined conversion for a type Item. This is what I've tried:
#include <iostream>
#include <boost/optional.hpp>
struct A
{
int x;
};
struct Item
{
boost::optional<int> x_;
Item(){}
Item(const A& s)
: x_(s.x)
{
}
operator boost::optional<A>() const {
boost::optional<A> s;
if (x_) {
s->x = *x_;
}
return s;
}
};
std::vector<A> getA(const std::vector<Item> &items) {
std::vector<A> a;
for (const auto &i : items) {
if (i.x_) {
a.push_back(*static_cast<boost::optional<A>>(i)); // <- this line causes error
}
}
return a;
}
That is how I use it:
int main() {
A a;
a.x = 3;
Item i(a);
auto v = getA({i});
return 0;
}
g++ -std=c++11 says:
In file included from /usr/include/boost/optional.hpp:15:0,
from test.cpp:2:
/usr/include/boost/optional/optional.hpp: In instantiation of ‘void boost::optional_detail::optional_base<T>::construct(const Expr&, const void*) [with Expr = Item; T = A]’:
/usr/include/boost/optional/optional.hpp:262:25: required from ‘boost::optional_detail::optional_base<T>::optional_base(const Expr&, const Expr*) [with Expr = Item; T = A]’
/usr/include/boost/optional/optional.hpp:559:78: required from ‘boost::optional<T>::optional(const Expr&) [with Expr = Item; T = A]’
test.cpp:30:55: required from here
/usr/include/boost/optional/optional.hpp:392:8: error: no matching function for call to ‘A::A(const Item&)’
new (m_storage.address()) internal_type(expr) ;
^
/usr/include/boost/optional/optional.hpp:392:8: note: candidates are:
test.cpp:3:8: note: A::A()
struct A
^
test.cpp:3:8: note: candidate expects 0 arguments, 1 provided
test.cpp:3:8: note: constexpr A::A(const A&)
test.cpp:3:8: note: no known conversion for argument 1 from ‘const Item’ to ‘const A&’
test.cpp:3:8: note: constexpr A::A(A&&)
test.cpp:3:8: note: no known conversion for argument 1 from ‘const Item’ to ‘A&&’
Why does it try to find A struct constructor instead of use user defined conversion operator?
You may point me directly to any position of the user-defined conversion page because I am unable to find any reason for this. For example,
User-defined conversion function is invoked on the second stage of the implicit conversion, which consists of zero or one converting constructor or zero or one user-defined conversion function.
in my opinion directly says that if no conversion constructor is defined then user-defined conversion function will be used. Am I wrong? And if yes, how can I implement user-defined conversion then without defining conversion cunstructor in struct A ?
You have two issues with your code. Your optional operator never initializes the boost::optional. If you don't do that, accessing members is undefined behavior. What you have to do is:
operator boost::optional<A>() const {
boost::optional<A> s;
if (x_) {
s = A{*x_};
}
return s;
}
The second issue is when you do:
static_cast<boost::optional<A>>(i);
That is equivalent to:
boost::optional<A> __tmp(i);
But it turns out that boost::optional has an explicit template constructor. That will be preferred to your conversion function. The error you're seeing is the compiling going down the path of this factory constructor, where Item is not such a factory.
You could simply use boost::optional<A> directly:
std::vector<A> getA(const std::vector<Item> &items) {
std::vector<A> a;
for (boost::optional<A> opt : items) {
if (opt) {
a.push_back(*opt);
}
}
return a;
}
Or, since the constructor template is explicit, you could use the conversion operator in a non-explicit context:
boost::optional<A> opt = i;
a.push_back(*opt);
This has the added benefit of also being easier to read.

std::swap for non copiable but movable struct

According to C++ reference std::swap is equivalent to
T c(std::move(a)); a=std::move(b); b=std::move(c);
This should allow to swap two non copiable but movable object. Therefore I don't understand why
#include<utility>
struct Foo {
Foo() = delete;
Foo(int) {};
Foo(Foo &) = delete;
Foo(Foo &&) {};
~Foo() {};
};
int main() {
Foo a(1),b(2);
std::swap(a,b);
}
is refused by the compiler with
In file included from /usr/include/c++/4.8/bits/stl_pair.h:59:0,
from /usr/include/c++/4.8/utility:70,
from swap.cpp:1:
/usr/include/c++/4.8/bits/move.h: In instantiation of ‘void std::swap(_Tp&, _Tp&) [with _Tp = Foo]’:
swap.cpp:13:16: required from here
/usr/include/c++/4.8/bits/move.h:176:11: error: use of deleted function ‘Foo& Foo::operator=(const Foo&)’
__a = _GLIBCXX_MOVE(__b);
^
swap.cpp:3:8: note: ‘Foo& Foo::operator=(const Foo&)’ is implicitly declared as deleted because ‘Foo’ declares a move constructor or move assignment operator
struct Foo {
^
In file included from /usr/include/c++/4.8/bits/stl_pair.h:59:0,
from /usr/include/c++/4.8/utility:70,
from swap.cpp:1:
/usr/include/c++/4.8/bits/move.h:177:11: error: use of deleted function ‘Foo& Foo::operator=(const Foo&)’
__b = _GLIBCXX_MOVE(__tmp);
note: this is with GCC 4.8 and 4.9 but clang complain as well.
You declared a move constructor. However, you need a move assignment operator for std::swap. You should add the following two operators:
auto operator=(const Foo& rhs) & -> Foo& = delete;
auto operator=(Foo&& rhs) & noexcept -> Foo&
{
// ...
return *this;
}

swap std::unique_ptr with lambda as deleter -- GCC

Can we use a lambda as a deleter with a std::unique_ptr ? Actualy, I did it with clang++ and it was happy to do so.
I'm using std::swap to swap to std::unique_ptr<ObjType, decltyp(deleter)>; where auto deleter = [](struct addrinfo* ptr){if (ptr != nullptr) {freeaddrinfo(ptr);} };. Clang's swap seems to do not need a copy assignment operator, but gcc's std::swap did, as you can see in those logs :
In file included from /usr/include/c++/4.8.1/memory:81:0,
from /home/zenol/proj/src/PROJ/TCPClient.cpp:28:
/usr/include/c++/4.8.1/bits/unique_ptr.h: In instantiation of ‘std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = addrinfo; _Dp = Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0]’:
/usr/include/c++/4.8.1/bits/move.h:176:11: required from ‘void std::swap(_Tp&, _Tp&) [with _Tp = std::unique_ptr<addrinfo, Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0>]’
/home/zenol/proj/src/Proj/SocketHelp.hpp:109:50: required from ‘void Proj::retrieve_addresses(std::string, int, addrinfo&, addrinfo*&, T&, U) [with T = std::unique_ptr<addrinfo, Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0>; U = Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0; std::string = std::basic_string<char>]’
/home/zenol/proj/src/PROJ/TCPClient.cpp:65:49: required from here
/usr/include/c++/4.8.1/bits/unique_ptr.h:193:16: erreur: use of deleted function ‘Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0& Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0::operator=(const Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0&)’
get_deleter() = std::forward<deleter_type>(__u.get_deleter());
^
/home/zenol/proj/src/Proj/TCPClient.cpp:56:21: note: a lambda closure type has a deleted copy assignment operator
auto deleter = [](struct addrinfo* ptr)
^
What says the standard? Can I manage to wap those two std::unique_ptr ? Are they a workaround ? (Maybe encapsulating the lambda inside a std::function? ...)
Edit :
Here is a small example that should be more or less the same thing :
auto deleter = [](struct addrinfo* ptr)
{if (ptr != nullptr) {freeaddrinfo(ptr);} };
std::unique_ptr<struct addrinfo, decltype(deleter)>
resources_keeper(nullptr, deleter);
int main()
{
decltype(resources_keeper) plouf1(nullptr, deleter);
decltype(resources_keeper) plouf2(nullptr, deleter);
std::swap(plouf1, plouf2);
return 0;
}
The error :
In file included from /usr/include/c++/4.8.1/bits/stl_pair.h:59:0,
from /usr/include/c++/4.8.1/bits/stl_algobase.h:64,
from /usr/include/c++/4.8.1/memory:62,
from mini.cpp:1:
/usr/include/c++/4.8.1/bits/move.h: In instantiation of ‘void std::swap(_Tp&, _Tp&) [with _Tp = __lambda0]’:
/usr/include/c++/4.8.1/tuple:381:36: required from ‘void std::_Tuple_impl<_Idx, _Head, _Tail ...>::_M_swap(std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with long unsigned int _Idx = 1ul; _Head = __lambda0; _Tail = {}]’
/usr/include/c++/4.8.1/tuple:382:35: required from ‘void std::_Tuple_impl<_Idx, _Head, _Tail ...>::_M_swap(std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with long unsigned int _Idx = 0ul; _Head = addrinfo*; _Tail = {__lambda0}]’
/usr/include/c++/4.8.1/tuple:667:33: required from ‘void std::tuple<_T1, _T2>::swap(std::tuple<_T1, _T2>&) [with _T1 = addrinfo*; _T2 = __lambda0]’
/usr/include/c++/4.8.1/tuple:1050:7: required from ‘void std::swap(std::tuple<_Elements ...>&, std::tuple<_Elements ...>&) [with _Elements = {addrinfo*, __lambda0}]’
/usr/include/c++/4.8.1/bits/unique_ptr.h:269:21: required from ‘void std::unique_ptr<_Tp, _Dp>::swap(std::unique_ptr<_Tp, _Dp>&) [with _Tp = addrinfo; _Dp = __lambda0]’
/usr/include/c++/4.8.1/bits/unique_ptr.h:484:7: required from ‘void std::swap(std::unique_ptr<_Tp, _Dp>&, std::unique_ptr<_Tp, _Dp>&) [with _Tp = addrinfo; _Dp = __lambda0]’
mini.cpp:21:29: required from here
/usr/include/c++/4.8.1/bits/move.h:176:11: erreur: use of deleted function ‘__lambda0& __lambda0::operator=(const __lambda0&)’
__a = _GLIBCXX_MOVE(__b);
^
mini.cpp:9:17: note: a lambda closure type has a deleted copy assignment operator
auto deleter = [](struct addrinfo* ptr)
^
In file included from /usr/include/c++/4.8.1/bits/stl_pair.h:59:0,
from /usr/include/c++/4.8.1/bits/stl_algobase.h:64,
from /usr/include/c++/4.8.1/memory:62,
from mini.cpp:1:
/usr/include/c++/4.8.1/bits/move.h:177:11: erreur: use of deleted function ‘__lambda0& __lambda0::operator=(const __lambda0&)’
__b = _GLIBCXX_MOVE(__tmp);
^
This has nothing to do with unique_ptr or tuple, you can reduce the error to this:
int main()
{
auto deleter = []() { };
auto del2 = deleter;
deleter = static_cast<decltype(deleter)>(del2);
}
Which compiles with Clang but fails with G++, giving this error:
t.cc: In function ‘int main()’:
t.cc:5:11: error: use of deleted function ‘main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&)’
deleter = static_cast<decltype(deleter)>(del2);
^
t.cc:3:19: note: a lambda closure type has a deleted copy assignment operator
auto deleter = []() { };
^
The last C++11 standard says in [expr.prim.lambda]/19:
The closure type associated with a lambda-expression has a deleted (8.4.3) default constructor and a deleted copy assignment operator. It has an implicitly-declared copy constructor (12.8) and may have an implicitly-declared move constructor (12.8).
So it is up to the compiler whether the type is move-assignable or not.
To expand on Jonathan Wakely's answer:
When you swap to unique_ptrs, you also have to swap their deleters. The problem you are seeing boils down to this: clang can swap two lambdas of the same type, gcc cannot (and the standard allows both as Jonathan quotes it). Demonstration:
#include <utility>
int main() {
auto f = [](){};
auto g(f);
std::swap(f, g);
}
This code works with clang but fails to compile with gcc. (And that is OK.)
That is why it is happening.
I suggest the following:
#include <memory>
#include <utility>
struct addrinfo { };
void freeaddrinfo(addrinfo* ) { }
struct deleter {
void operator()(struct addrinfo* ptr) {
if (ptr != nullptr)
freeaddrinfo(ptr);
}
};
using resources_keeper = std::unique_ptr<struct addrinfo, deleter>;
int main() {
resources_keeper plouf1(nullptr);
resources_keeper plouf2(nullptr);
std::swap(plouf1, plouf2);
return 0;
}
Note that the code became cleaner and more readable as well.
If you absolutely have to solve this with lambdas, then perhaps you could try something hackish like this: Swap only the pointers but not the deleters.
#include <iostream>
#include <memory>
#include <utility>
using namespace std;
template <class T, class D>
void swap_pointers_but_not_deleters(unique_ptr<T,D>& x, unique_ptr<T,D>& y) noexcept {
T* x_ptr = x.release();
x.reset(y.release());
y.reset(x_ptr);
}
int main() {
auto deleter = [](int* p){ delete p; };
unique_ptr<int,decltype(deleter)> a(new int(1),deleter);
unique_ptr<int,decltype(deleter)> b(new int(2),deleter);
swap_pointers_but_not_deleters(a, b);
cout << "a = " << *a << ", b = " << *b << endl;
}
Although this code seems to work, I really don't like it. I suggest the first solution that does not use lambdas.
I can reproduce a similar error with the following code:
struct A
{
A() = default;
A(A&&) = default;
//A & operator=(A&&) = default;
A(A const & ) = delete;
};
int main()
{
A a, b;
std::swap(a,b);
}
Uncomment the move assignment operator and the error goes away. I'm guessing gcc doesn't allow move assignment of lambas (I'm using version 4.7.2). Change the lambda to an actual function or functor and you should be alright.
As it turns out, you can solve it with lambdas, as long as they can be converted to function pointers (lambdas capturing nothing).
#include <memory>
#include <utility>
struct addrinfo { };
void freeaddrinfo(addrinfo* ) { }
auto deleter = [](struct addrinfo* ptr) {
if (ptr != nullptr)
freeaddrinfo(ptr);
};
using resources_keeper = std::unique_ptr<struct addrinfo, void(*)(struct addrinfo*)>;
int main() {
resources_keeper plouf1(nullptr,deleter);
resources_keeper plouf2(nullptr,deleter);
std::swap(plouf1, plouf2);
return 0;
}
However, I still like my other solution with the struct better. It is likely to be the most efficient one (thanks to inlining), followed by the solution presented here. Passing a heavy-weight std::function looks like an overkill to me if the deleter implementation is really simple. Whether these performance considerations matter, it is the profiler's job to tell.