Modify

Opened 2 years ago

Closed 2 years ago

#11285 closed Bugs (fixed)

Variant: apply_visitor broken with C ++14 and const references

Reported by: Roger Leigh <rleigh@…> Owned by: Antony Polukhin
Milestone: Boost 1.59.0 Component: variant
Version: Boost 1.58.0 Severity: Regression
Keywords: Cc: antoshkka@…

Description

This is broken with C++14 with clang++, and Boost 1.58. It was working a week back with Boost 1.57 prior to homebrew upgrading Boost to 1.58; I'll double-check this wasn't due to XCode also being upgraded, though I don't think that's a problem--I'd been using -std=c++14 routinely for some months. Hence I've put this down as a regression, but there's a little uncertainly there.

Sample minimal testcase:

#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/variant.hpp>

#include <iostream>
#include <string>

struct a
{
  const std::string& type() const
  {
    static const std::string v("a");
    return v;
  }
};

struct b
{
  const std::string& type() const
  {
    static const std::string v("b");
    return v;
  }
};

typedef boost::variant<a,b> ab;

struct TestVisitor : public boost::static_visitor<const std::string&>
  {
    template <typename T>
    const std::string&
    operator() (const T& v)
    {
      return v.type();
    }
  };

int
main()
{
  ab val = a();
  TestVisitor v;
  std::cout << boost::apply_visitor(v, val) << std::endl;
}

With clang++ on MacOS 10.10:

% clang++ --version
Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix

# Success for C++98:
% clang++ -o test test.cpp
% ./test
a

# Success for C++11
% clang++ -std=c++11 -o test test.cpp
% ./test
a

# Failure for C++14
% clang++ -std=c++14 -o test test.cpp
test.cpp:42:16: error: call to 'apply_visitor' is ambiguous
  std::cout << boost::apply_visitor(v, val) << std::endl;
               ^~~~~~~~~~~~~~~~~~~~
/usr/local/include/boost/variant/detail/apply_visitor_unary.hpp:68:1: note:
      candidate function [with Visitor = TestVisitor, Visitable = boost::variant<a,
      b>]
apply_visitor(Visitor& visitor, Visitable& visitable)
^
/usr/local/include/boost/variant/detail/apply_visitor_unary.hpp:150:23: note:
      candidate function [with Visitor = TestVisitor, Visitable = boost::variant<a,
      b>]
inline decltype(auto) apply_visitor(Visitor& visitor, Visitable& visitable,
                      ^
/usr/local/include/boost/variant/detail/apply_visitor_unary.hpp:82:1: note:
      candidate function [with Visitor = TestVisitor, Visitable = boost::variant<a,
      b>]
apply_visitor(const Visitor& visitor, Visitable& visitable)
^
/usr/local/include/boost/variant/detail/apply_visitor_unary.hpp:160:23: note:
      candidate function [with Visitor = TestVisitor, Visitable = boost::variant<a,
      b>]
inline decltype(auto) apply_visitor(const Visitor& visitor, Visitable& visitable,
                      ^
1 error generated.

Attachments (0)

Change History (6)

comment:1 Changed 2 years ago by Roger Leigh <rleigh@…>

I have tested on FreeBSD 10.1 with clang++34, clang++35 and clang++36 with all combinations of -std=c++(98|11|14) and the code compiles and runs in all cases with Boost 1.55, 1.56 and 1.57.

With Boost 1.58 it fails with -std=c++14 for both clang++35 and clang++36 (clang++34 does not support c++14).

To summarise:

  • The regression was introduced between 1.57 and 1.58
  • This is trivially reproducible with clang++ 3.5 or 3.6 with -std=c++14

Regards, Roger

comment:2 Changed 2 years ago by Roger Leigh <rleigh@…>

Unreproducible with GCC5; looks specific to clang++/libc++.

comment:3 Changed 2 years ago by anonymous

Workaround: manually inline the visitable.apply_visitor(visitor) call from the body of apply_visitor().

comment:4 Changed 2 years ago by Roger Leigh <rleigh@…>

Note: #11251 is the same problem I think.

comment:5 Changed 2 years ago by Antony Polukhin

Cc: antoshkka@… added
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<const std::string&> or just patch the `boost/variant/detail/has_result_type.hpp ` file.

comment:6 Changed 2 years ago by Antony Polukhin

Resolution: fixed
Status: assignedclosed

Fixed in Boost 1.59.0

Modify Ticket

Change Properties
Set your email in Preferences
Action
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.