Opened 3 years ago

Closed 3 years ago

#11251 closed Bugs (fixed)

boost::static_visitor<T&> causes a compilation error in C++14 mode

Reported by: bogdan <inadgob@…> Owned by: Antony Polukhin
Milestone: Boost 1.59.0 Component: variant
Version: Boost 1.58.0 Severity: Regression
Keywords: variant visitor reference return Cc:


Consider the following code:

#include <iostream>
#include "boost/variant.hpp"

struct test_vis : boost::static_visitor<int&>
   int& operator()(int& i) const { return i; }

int main()
   boost::variant<int> dummy;
   ++boost::apply_visitor(test_vis(), dummy);
   std::cout << dummy << '\n';

Clang 3.6.0 compiles the code with no diagnostics in C++11 mode, but in C++14 mode it issues the following error: error: call to 'apply_visitor' is ambiguous
   ++boost::apply_visitor(test_vis(), dummy);
/boost/variant/detail/apply_visitor_unary.hpp:82:1: note: candidate function [with Visitor = test_vis, Visitable = boost::variant<int>]
apply_visitor(const Visitor& visitor, Visitable& visitable)
/boost/variant/detail/apply_visitor_unary.hpp:160:23: note: candidate function [with Visitor = test_vis, Visitable = boost::variant<int>]
inline decltype(auto) apply_visitor(const Visitor& visitor, Visitable& visitable,
1 error generated.

The same behaviour occurs in GCC 5.1.0. MSVC12 compiles the code, MSVC14 issues a similar error.

This is due to the fact that the apply_visitor overloads using automatic return type detection are not disabled in this case, because the boost::detail::variant::has_result_type trait is not working properly for reference types. Internally, it uses an overload with a parameter type of typename C::result_type*, which tries to form a pointer to reference type in this case, which causes a deduction failure.

A quick workaround is to remove the base class for the visitor in C++14 mode in this case.

Attachments (0)

Change History (3)

comment:1 Changed 3 years ago by James Benze <benzejaa@…>

I can duplicate this error with GCC 5.1.0

Thanks for the quick workaround. Much appreciated.

comment:2 Changed 3 years ago by Antony Polukhin

Milestone: To Be DeterminedBoost 1.59.0
Owner: changed from ebf to Antony Polukhin
Status: newassigned

Thanks for the report and hints!

This issue has been fixed in develop branch. As a workaround you can remove the boost::static_visitor<int&> or just patch the `boost/variant/detail/has_result_type.hpp ` file.

comment:3 Changed 3 years ago by Antony Polukhin

Resolution: fixed
Status: assignedclosed

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain Antony Polukhin.
The resolution will be deleted.

Add Comment

E-mail address and name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.