mirror of
https://github.com/lighttransport/tinyusdz.git
synced 2026-01-18 01:11:17 +01:00
Add variant::cast()
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "simple-variant.hh"
|
||||
#include "tiny-variant.hh"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
@@ -17,11 +18,15 @@ int main(int argc, char **argv) {
|
||||
|
||||
a.set<float>(1.3f);
|
||||
|
||||
std::cout << "a val = " << a.get<float>() << "\n";
|
||||
std::cout << "a val = " << a.cast<float>() << "\n";
|
||||
|
||||
if (auto v = b.get_if<float>()) {
|
||||
std::cout << "b val = " << (*v) << "\n";
|
||||
}
|
||||
|
||||
if (auto v = b.get<float>()) {
|
||||
std::cout << "b val = " << v.value() << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -262,11 +262,12 @@ struct variant {
|
||||
set<T>(v);
|
||||
}
|
||||
|
||||
#if 0
|
||||
#if 1
|
||||
// allow seg fault.
|
||||
template <typename T, typename... Args,
|
||||
typename =
|
||||
typename std::enable_if<is_one_of<T, Ts...>::value, void>::type>
|
||||
T& get() {
|
||||
T& cast() {
|
||||
// It is a dynamic_cast-like behaviour
|
||||
if (variant_id == value::TypeTrait<T>::type_id) {
|
||||
return *reinterpret_cast<T*>(&data);
|
||||
|
||||
Reference in New Issue
Block a user