<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix"> Hi Gabor,<br>
<br>
Passing cast operation as a template argument looks
overcomplicated to me. However, it can be used as a base for other
operations, so I think it is worth it to include this as an
overload. It can also be useful if an operation other than cast is
required, but, unfortunately, I cannot remember any case where we
need such behaviour.<br>
The problem is that I cannot compile your sample. I tried to write
this:<br>
<br>
<tt> /// Helper for importing Decls as Optionals.</tt><tt><br>
</tt><tt> template <template <typename> class CastOp,
typename DeclTy></tt><tt><br>
</tt><tt> Optional<DeclTy *> import(DeclTy *FromD) {</tt><tt><br>
</tt><tt> if (auto Res = Importer.Import(FromD))</tt><tt><br>
</tt><tt> return CastOp<DeclTy>(*Res);</tt><tt><br>
</tt><tt> return None;</tt><tt><br>
</tt><tt> }</tt><tt><br>
</tt><tt> </tt><tt><br>
</tt><tt> template <typename DeclTy></tt><tt><br>
</tt><tt> Optional<DeclTy *> importNullable(DeclTy
*FromD) {</tt><tt><br>
</tt><tt> return import<cast_or_null>(FromD);</tt><tt><br>
</tt><tt> }</tt><tt><br>
</tt><tt> </tt><tt><br>
</tt><tt> template <typename DeclTy> Optional<DeclTy
*> importNonNull(DeclTy *FromD) {</tt><tt><br>
</tt><tt> return import<cast>(FromD);</tt><tt><br>
</tt><tt> }<br>
<br>
<br>
</tt>But there are two issues. First, cast<> has two
template arguments, not one. Second, template instantiation cannot
guess what overload of cast should be used in each case. I'm
sorry, but after a few attempts of making this code to compile, I
have to ask you to elaborate a bit more once again.<br>
<br>
Regarding naming: what do you think about importNonNull[Into]()
and importNullable[Into]()? So, we assume that import should
return the same type as the argument (which, I think, is somewhat
expected). Or should we reflect the cast it in the function name
somehow?<br>
<br>
Currently, the list of helpers look like this:<br>
<br>
<tt> template <typename DeclTy></tt><tt><br>
</tt><tt> LLVM_NODISCARD bool importNullableInto(DeclTy
*&ToD, Decl *FromD);</tt><tt><br>
</tt><tt> </tt><tt><br>
</tt><tt> template <typename DeclTy></tt><tt><br>
</tt><tt> LLVM_NODISCARD bool importNonNullInto(DeclTy
*&ToD, Decl *FromD);</tt><tt><br>
</tt><tt> </tt><tt><br>
</tt><tt> template <typename DeclTy></tt><tt><br>
</tt><tt> Optional<DeclTy *> importNullable(DeclTy
*FromD);</tt><tt><br>
</tt><tt> </tt><tt><br>
</tt><tt> template <typename DeclTy> Optional<DeclTy
*> importNonNull(DeclTy *FromD);</tt><br>
<br>
There is also another thing I am curious about: do we need to use
dyn_casts on the return type? I'm not sure we can return a node
with type so different from the imported node's type. Currently,
there are some dyn_cast[_or_null]s in our code, but, possibly,
they can be replaced with just casts, because we usually return
nullptr or a pointer of same type as the imported one.<br>
<br>
<br>
18.05.2018 22:47, Gábor Márton пишет:<br>
</div>
<blockquote type="cite"
cite="mid:CAH6rKyAZ_wGpN6umxX5Cw0PmgDmrf00=B=e_Ubv2y9RE2OtiYQ@mail.gmail.com">
<div dir="ltr">Of course, I think there is no need to have
different functions which differ only in the cast operation. We
can use a template template parameter for the cast operation:<br>
<br>
<font face="monospace, monospace"> template
<template<typename> class CastOp, typename DeclTy><br>
LLVM_NODISCARD bool import(DeclTy *&ToD, DeclTy
*FromD) {<br>
if (auto Res = Importer.Import(FromD)) {<br>
ToD = CastOp<DeclTy>(*Res);<br>
return false;<br>
}<br>
return true;<br>
}<br>
</font><br>
And then the usage:<br>
<font face="monospace, monospace"> if
(import<cast_or_null>(ToEPI.ExceptionSpec.SourceDecl,<br>
FromEPI.ExceptionSpec.SourceDecl))<br>
return {};</font>
<div><br>
</div>
<div>Gabor</div>
<div><br>
</div>
<div>On Fri, May 18, 2018 at 8:52 PM Aleksei Sidorin <<a
href="mailto:a.sidorin@samsung.com" moz-do-not-send="true">a.sidorin@samsung.com</a>>
wrote:<br>
><br>
> Sorry, I didn't got the idea. Could please you explain a
bit more?<br>
><br>
> 18.05.2018 13:09, Gábor Márton via cfe-dev пишет:<br>
> > An other idea, couldn't we just pass the cast
operation as a template<br>
> > template parameter?<br>
> > _______________________________________________<br>
> > cfe-dev mailing list<br>
> > <a href="mailto:cfe-dev@lists.llvm.org"
moz-do-not-send="true">cfe-dev@lists.llvm.org</a><br>
> > <a
href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev"
moz-do-not-send="true">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
><br>
><br>
> --<br>
> Best regards,<br>
> Aleksei Sidorin,<br>
> SRR, Samsung Electronics<br>
><br>
</div>
</div>
</blockquote>
<p><br>
</p>
</body>
</html>