[PATCH] D59142: YAMLIO: Improve template arg deduction for mapOptional
Pavel Labath via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 14 05:32:56 PDT 2019
labath updated this revision to Diff 190605.
labath added a comment.
Use the static_assert approach to check for convertibility. The only change I
made was to static_cast the default value into a `const T &` instead of a plain
`T`, as the latter would force a copy even if the default value was already of
the same type.
(Interestingly, the static_cast to a reference type is already enough to
disqualify explicit conversions for gcc and msvc. Clang still permits those, but
I have no idea who is correct here.)
Repository:
rL LLVM
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D59142/new/
https://reviews.llvm.org/D59142
Files:
include/llvm/Support/YAMLTraits.h
unittests/Support/YAMLIOTest.cpp
Index: unittests/Support/YAMLIOTest.cpp
===================================================================
--- unittests/Support/YAMLIOTest.cpp
+++ unittests/Support/YAMLIOTest.cpp
@@ -823,7 +823,7 @@
io.mapRequired("f1", c.f1);
io.mapRequired("f2", c.f2);
io.mapRequired("f3", c.f3);
- io.mapOptional("f4", c.f4, MyFlags(flagRound));
+ io.mapOptional("f4", c.f4, flagRound);
}
};
}
@@ -1327,8 +1327,8 @@
static void mapping(IO &io, TotalSeconds &secs) {
MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
- io.mapOptional("hours", keys->hours, (uint32_t)0);
- io.mapOptional("minutes", keys->minutes, (uint8_t)0);
+ io.mapOptional("hours", keys->hours, 0);
+ io.mapOptional("minutes", keys->minutes, 0);
io.mapRequired("seconds", keys->seconds);
}
};
Index: include/llvm/Support/YAMLTraits.h
===================================================================
--- include/llvm/Support/YAMLTraits.h
+++ include/llvm/Support/YAMLTraits.h
@@ -863,8 +863,8 @@
mapOptionalWithContext(Key, Val, Ctx);
}
- template <typename T>
- void mapOptional(const char *Key, T &Val, const T &Default) {
+ template <typename T, typename DefaultT>
+ void mapOptional(const char *Key, T &Val, const DefaultT &Default) {
EmptyContext Ctx;
mapOptionalWithContext(Key, Val, Default, Ctx);
}
@@ -890,10 +890,13 @@
this->processKey(Key, Val, false, Ctx);
}
- template <typename T, typename Context>
- void mapOptionalWithContext(const char *Key, T &Val, const T &Default,
+ template <typename T, typename Context, typename DefaultT>
+ void mapOptionalWithContext(const char *Key, T &Val, const DefaultT &Default,
Context &Ctx) {
- this->processKeyWithDefault(Key, Val, Default, false, Ctx);
+ static_assert(std::is_convertible<DefaultT, T>::value,
+ "Default type must be implicitly convertible to value type!");
+ this->processKeyWithDefault(Key, Val, static_cast<const T &>(Default),
+ false, Ctx);
}
private:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59142.190605.patch
Type: text/x-patch
Size: 2141 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190314/dc6a3912/attachment.bin>
More information about the llvm-commits
mailing list