Skip to content

Visitors can't return references, but they can with std::variant #39

@ligfx

Description

@ligfx

Example of the issue:

#define variant_CONFIG_SELECT_VARIANT 1
#include <variant>
#include "nonstd/variant.hpp"
#include <string>


struct tag_t {};

struct myvisitor {
  const std::string& operator()(const int&) const { throw std::exception(); }
  const std::string& operator()(const tag_t&) const { throw std::exception(); }
  const std::string& operator()(const std::string& s) const { return s; }
};

void test_std() {
   std::variant<int, tag_t, std::string> v("hello");
   std::visit(myvisitor(), v);
}

void test_nonstd() {
   nonstd::variant<int, tag_t, std::string> v("hello");
   nonstd::visit(myvisitor(), v);
}

int main() {
   test_std();
   test_nonstd();
}

The STL version of variant compiles and works. nonstd::variant throws a compile error:

./nonstd/variant.hpp:1925:16: error: reference to type 'const std::__1::basic_string<char>' requires an initializer
        return R();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions