@@ -1075,6 +1075,75 @@ test_common()
1075
1075
static_assert (s5.get <3 >() == 3 );
1076
1076
}
1077
1077
1078
+ void
1079
+ test_visit ()
1080
+ {
1081
+ using variant_t = std::variant<int , bool , std::string, double >;
1082
+
1083
+ variant_t variant = 10 ;
1084
+ size_t test_count = 0 ;
1085
+ tnt::visit ([&](const auto &value) {
1086
+ using value_t = std::remove_cv_t <std::remove_reference_t <decltype (value)>>;
1087
+ if constexpr (std::is_same_v<value_t , int >) {
1088
+ fail_unless (value == 10 );
1089
+ test_count++;
1090
+ }
1091
+ }, variant);
1092
+
1093
+ variant = true ;
1094
+ tnt::visit ([&](const auto &value) {
1095
+ using value_t = std::remove_cv_t <std::remove_reference_t <decltype (value)>>;
1096
+ if constexpr (std::is_same_v<value_t , bool >) {
1097
+ fail_unless (value);
1098
+ test_count++;
1099
+ }
1100
+ }, variant);
1101
+
1102
+ variant = std::string (" abc" );
1103
+ tnt::visit ([&](const auto &value) {
1104
+ using value_t = std::remove_cv_t <std::remove_reference_t <decltype (value)>>;
1105
+ if constexpr (std::is_same_v<value_t , std::string>) {
1106
+ fail_unless (value == " abc" );
1107
+ test_count++;
1108
+ }
1109
+ }, variant);
1110
+
1111
+ variant = 3.0 ;
1112
+ tnt::visit ([&](const auto &value) {
1113
+ using value_t = std::remove_cv_t <std::remove_reference_t <decltype (value)>>;
1114
+ if constexpr (std::is_same_v<value_t , double >) {
1115
+ fail_unless (value == 3.0 );
1116
+ test_count++;
1117
+ }
1118
+ }, variant);
1119
+
1120
+ /* Check if all the tests were fired. */
1121
+ fail_unless (test_count == 4 );
1122
+
1123
+ test_count = 0 ;
1124
+ CustomVariant custom_variant;
1125
+ custom_variant.template emplace <0 >(42 );
1126
+ tnt::visit ([&](const auto &value) {
1127
+ using value_t = std::remove_cv_t <std::remove_reference_t <decltype (value)>>;
1128
+ if constexpr (std::is_same_v<value_t , int >) {
1129
+ fail_unless (value == 42 );
1130
+ test_count++;
1131
+ }
1132
+ }, custom_variant);
1133
+
1134
+ custom_variant.template emplace <1 >(66.6 );
1135
+ tnt::visit ([&](const auto &value) {
1136
+ using value_t = std::remove_cv_t <std::remove_reference_t <decltype (value)>>;
1137
+ if constexpr (std::is_same_v<value_t , double >) {
1138
+ fail_unless (value == 66.6 );
1139
+ test_count++;
1140
+ }
1141
+ }, custom_variant);
1142
+
1143
+ /* Check if all the tests were fired. */
1144
+ fail_unless (test_count == 2 );
1145
+ }
1146
+
1078
1147
int main ()
1079
1148
{
1080
1149
test_integer_traits ();
@@ -1089,4 +1158,5 @@ int main()
1089
1158
test_rw_container_traits ();
1090
1159
test_is_method_callable ();
1091
1160
test_common ();
1161
+ test_visit ();
1092
1162
}
0 commit comments