At the llvm or mlir, the code : (false) ? ::mlir::Builder((*this)->getContext()).getUnitAttr() : nullptr

Hi
a little curious, at llvm or mlir, usually meet the code as below:
(false) ? ::mlir::Builder((*this)->getContext()).getUnitAttr() : nullptr

why not directly use nullptr in place of, thanks

I doubt you’ll find this in source code, you may however see this kind of things in generated code. It’s pretty frequent with code-generator that follow a simple template: we don’t want to complicate the template code generator and instead let the C++ compiler optimize this out.

yes, the above code is at the generated code. at the libcxx common_type.h, have the below code:

template <class _Tp, class _Up>
struct __common_type2_imp<_Tp, _Up, __void_t<decltype(true ? std::declval<_Tp>() : std::declval<_Up>())> > {

typedef _LIBCPP_NODEBUG __decay_t<decltype(true ? std::declval<_Tp>() : std::declval<_Up>())> type;
};

it also have: true ? std::declval<_Tp>() : std::declval<_Up>(), I guess the purpose is let c++ compiler instantiate _Tp and _Up, then optimize it out, if is wrong, pls correct me, thanks.

I don’t understand the code in libc++, that’s a question you should ask in the relevant category (I would be tempted to say that here it may impact SFINAE?)

thanks, later I will submit this question at libcxx category.