3

This question is related to Type erasing type erasure, `any` questions?

Given the class wstr below (just a string wrapper)

class wstr { public: std::string text; }; 

I have an any...

std::any myAny({myWstr}); 

..and I want to cast (convert) it to a string..

std::string myString = std::any_cast<std::string>(myAny); 

... Is there a means of doing this by using template specialization, or (as I suspect) is this missing the point of using std::any?

This question is not about implicit conversion. I was thinking that I may need to write a friend function/overloading the cast, similar to writing ostream operator extensions.

Another way of asking this would be: Am I correct in thinking an std::any_cast does NOT CAST to anything, but rather ONLY CASTS a std::any back to it's original form, and therefore one cannot overload a function that supports the cast to eg, std::string, and is not available (for some reason) for friend function overloading / template specialization?

wstr myWstr("foo"); std::any myAny({myWstr}); wstr myWstr = std::any_cast<wstr>(myAny); //is okay. std::string mMytr = std::any_cast<std::string>(myAny); //No overloads! 
12
  • 4
    See my question on the subject a while back. You cannot any_cast to another type. Commented Jan 22, 2019 at 12:17
  • 7
    std::any<wstr> is wrong, because std::any isn't a template. Are you perhaps thinking about std::variant or std::optional? Commented Jan 22, 2019 at 12:19
  • 5
    afaik any_cast is not really to cast between different types but merely to access the value stored in the any in a typesafe manner Commented Jan 22, 2019 at 12:26
  • 2
    It does indeed look like you are missing the point of std::any. What are you trying to achieve exactly? And what do you think std::any does? Commented Jan 22, 2019 at 13:10
  • 2
    @Konchog: The thing is, you shouldn't want to "overload/specialize" any_cast. If that's what you want to do, then you are probably using any outside of its intended use case and purpose. Commented Jan 22, 2019 at 14:24

1 Answer 1

2

The example given for any_cast at cppreference shows that casting an int to a std::string results in bad_any_cast exception being thrown.

The result will be the same if any_cast is used to convert wstr to std::string.

And BTW, std::any<wstr> myAny; does not work, since std::any is a class and not a template.

Instead you can test it this way:

#include <string> #include <iostream> #include <any> class wstr { public: std::string text; }; int main() { wstr ws; auto a = std::any(ws); try { std::cout << std::any_cast<std::string>(a) << '\n'; } catch(const std::bad_any_cast& e) { std::cout << e.what() << '\n'; } } 

See demo here.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but due to the poverty of my initial question, this misses the point.. the point I was making was concerning extending/overloading std::any_cast, not expecting it to do the cast as it stands.
@Konchog: then you will have to write your own conversion function, constructor or operator. std::any_cast is not the way to do it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.