// Copyright 2017 <Biagio Festa>
#include <iostream>
#include <string>
#include <utility>
using DateType = int;
class DateOrString {
public:
explicit DateOrString(std::string str)
: m_is_a_string(true), m_string(std::move(str)) {}
explicit DateOrString(DateType date) : m_date(std::move(date)) {}
const std::string& get_string() const {
if (m_is_a_string == false) {
throw std::runtime_error("It does not keep a string type");
}
return m_string;
}
const DateType& get_date() const {
if (m_is_a_string == true) {
throw std::runtime_error("It does not keep a date type");
}
return m_date;
}
bool is_string() const noexcept { return m_is_a_string; }
private:
bool m_is_a_string = false;
DateType m_date;
std::string m_string;
};
DateOrString api_call(const std::string& selector) {
if (selector.empty()) {
return DateOrString{"This is a string"};
}
return DateOrString{DateType{}};
}
int main() {
auto r1 = api_call("");
auto r2 = api_call("s");
std::cout << r1.get_string() << '\n';
std::cout << r2.get_date() << '\n';
return 0;
}
Ly8gQ29weXJpZ2h0IDIwMTcgPEJpYWdpbyBGZXN0YT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dXRpbGl0eT4KCnVzaW5nIERhdGVUeXBlID0gaW50OwoKY2xhc3MgRGF0ZU9yU3RyaW5nIHsKIHB1YmxpYzoKICBleHBsaWNpdCBEYXRlT3JTdHJpbmcoc3RkOjpzdHJpbmcgc3RyKQogICAgICA6IG1faXNfYV9zdHJpbmcodHJ1ZSksIG1fc3RyaW5nKHN0ZDo6bW92ZShzdHIpKSB7fQogIGV4cGxpY2l0IERhdGVPclN0cmluZyhEYXRlVHlwZSBkYXRlKSA6IG1fZGF0ZShzdGQ6Om1vdmUoZGF0ZSkpIHt9CgogIGNvbnN0IHN0ZDo6c3RyaW5nJiBnZXRfc3RyaW5nKCkgY29uc3QgewogICAgaWYgKG1faXNfYV9zdHJpbmcgPT0gZmFsc2UpIHsKICAgICAgdGhyb3cgc3RkOjpydW50aW1lX2Vycm9yKCJJdCBkb2VzIG5vdCBrZWVwIGEgc3RyaW5nIHR5cGUiKTsKICAgIH0K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