Peter
Peter

Reputation: 3165

What types are convertible to std::any?

While writing a clang plugin, I noticed that objects of type llvm::cl::opt<T> are not convertible to std::any, i.e. the following snippet does not compile:

#include <any>
#include <string>

#include "llvm/Support/CommandLine.h"

int main()
{
  llvm::cl::opt<std::string> opt("o");
  std::any opt_any = opt; // doesn't work!
}

I would like to know both why this isn't possible in this specific instance and what criteria a type has to fullfill in general in order to be convertible to std::any.

Upvotes: 0

Views: 105

Answers (1)

NathanOliver
NathanOliver

Reputation: 180650

llvm::cl::opt is not copyable. Both the copy constrcutor and copy assignment operator are marked as delete.

std::any requires that in construction and assignment that the type be copyable.


You could use a std::unique_ptr<llvm::cl::opt<std::string>> or std::shared_ptr<llvm::cl::opt<std::string>> or even a std::reference_wrapper depending on how you want to handle the lifetime of the object.

Upvotes: 5

Related Questions