OSDN Git Service

YAMLIO: Improve template arg deduction for mapOptional
authorPavel Labath <pavel@labath.sk>
Thu, 14 Mar 2019 15:23:40 +0000 (15:23 +0000)
committerPavel Labath <pavel@labath.sk>
Thu, 14 Mar 2019 15:23:40 +0000 (15:23 +0000)
commite287d6a791e06558575c29c1ba58123c9c0f49a2
tree2c6e95861c19e94fbfad2e32aa564d6ad1fe482c
parent9ec5e55104dde136aac5f120cbfb6a1f7546c6b4
YAMLIO: Improve template arg deduction for mapOptional

Summary:
The way c++ template argument deduction works, both arguments are used
to deduce the template type in the three-argument overload of
mapOptional. This is a problem if the types are slightly different, even
if they are implicitly convertible. This is fairly easy to trigger with
integral types, as the default type of most integral constants is int,
which then requires casting the constant to the type of the other
argument.

This patch fixes that by using a separate template type for the default
value, which is then cast to the type of the first argument.  To avoid
this conversion triggerring conversions marged as explicit, we use
static_assert to check that the types are implicitly convertible.

Reviewers: zturner, sammccall

Subscribers: kristina, jdoerfert, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D59142

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@356157 91177308-0d34-0410-b5e6-96231b3b80d8
include/llvm/Support/YAMLTraits.h
unittests/Support/YAMLIOTest.cpp