forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add std::variant backport as c10::variant (pytorch#26836)
Summary: Pull Request resolved: pytorch#26836 * **pytorch#26836 Add std::variant backport as c10::variant** Test Plan: Imported from OSS Differential Revision: D17649064 Pulled By: yf225 fbshipit-source-id: aa5ee26fe7078cc66d03663b9ff9e998e1d5839a
- Loading branch information
1 parent
cca3a36
commit 0cd1880
Showing
3 changed files
with
2,865 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <c10/util/variant.h> | ||
|
||
namespace testns { | ||
|
||
namespace enumtype { | ||
// NOTE: We need to provide the default constructor for each struct, | ||
// otherwise Clang 3.8 would complain: | ||
// ``` | ||
// error: default initialization of an object of const type 'const enumtype::Enum1' | ||
// without a user-provided default constructor | ||
// ``` | ||
struct Enum1 { Enum1() {} }; | ||
struct Enum2 { Enum2() {} }; | ||
struct Enum3 { Enum3() {} }; | ||
} // namespace enumtype | ||
|
||
const enumtype::Enum1 kEnum1; | ||
const enumtype::Enum2 kEnum2; | ||
const enumtype::Enum3 kEnum3; | ||
|
||
} // namespace testns | ||
|
||
std::string func(c10::variant<testns::enumtype::Enum1, testns::enumtype::Enum2, testns::enumtype::Enum3> v) { | ||
if (c10::get_if<testns::enumtype::Enum1>(&v)) { | ||
return "Enum1"; | ||
} else if (c10::get_if<testns::enumtype::Enum2>(&v)) { | ||
return "Enum2"; | ||
} else if (c10::get_if<testns::enumtype::Enum3>(&v)) { | ||
return "Enum3"; | ||
} else { | ||
return "Unsupported enum"; | ||
} | ||
} | ||
|
||
TEST(VariantTest, Basic) { | ||
ASSERT_EQ(func(testns::kEnum1), "Enum1"); | ||
ASSERT_EQ(func(testns::kEnum2), "Enum2"); | ||
ASSERT_EQ(func(testns::kEnum3), "Enum3"); | ||
} |
Oops, something went wrong.