Skip to content

Commit 2be60b9

Browse files
committed
Replace if constexpr with SFINAE
msvc having trouble
1 parent 489520d commit 2be60b9

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

src/backend/ScientificDefaults.cpp

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,36 @@ auto AttributeReader::operator()(
224224
return attribute_read_result::Success{};
225225
}
226226

227+
namespace
228+
{
229+
// Need SFINAE for this since MSVC doesnt understand if constexpr
230+
template <typename GetDefaultValue, typename SFINAE = void>
231+
struct CallGetDefaultValue
232+
{
233+
GetDefaultValue val;
234+
CallGetDefaultValue(GetDefaultValue val_in) : val(std::move(val_in))
235+
{}
236+
auto operator()() && -> GetDefaultValue
237+
{
238+
return std::move(val);
239+
}
240+
};
241+
242+
template <typename GetDefaultValue>
243+
struct CallGetDefaultValue<
244+
GetDefaultValue,
245+
std::void_t<std::invoke_result_t<GetDefaultValue>>>
246+
{
247+
GetDefaultValue val;
248+
CallGetDefaultValue(GetDefaultValue val_in) : val(std::move(val_in))
249+
{}
250+
auto operator()() && -> std::invoke_result_t<GetDefaultValue>
251+
{
252+
return std::move(val)();
253+
}
254+
};
255+
} // namespace
256+
227257
// 4. ConfigAttribute class implementations
228258
ConfigAttribute::ConfigAttribute(
229259
Attributable &child_in, char const *attrName_in)
@@ -240,23 +270,18 @@ auto ConfigAttribute::withSetter(
240270
detail::CallResult_t<GetDefaultValue>,
241271
S>> setDefaultVal) -> ConfigAttribute &
242272
{
243-
initDefaultAttribute = [getDefaultVal_lambda =
244-
std::forward<GetDefaultValue>(getDefaultVal),
245-
setDefaultVal](Attributable &attr) {
246-
RecordType *record = dynamic_cast<RecordType *>(&attr);
247-
if (!record)
248-
{
249-
throw error::Internal("dynamic cast failure");
250-
}
251-
if constexpr (detail::IsCallable_v<GetDefaultValue>)
252-
{
253-
((*record).*setDefaultVal)(getDefaultVal_lambda());
254-
}
255-
else
256-
{
257-
((*record).*setDefaultVal)(std::move(getDefaultVal_lambda));
258-
}
259-
};
273+
initDefaultAttribute =
274+
[callDefaultValue =
275+
CallGetDefaultValue<std::remove_reference_t<GetDefaultValue>>(
276+
std::forward<GetDefaultValue>(getDefaultVal)),
277+
setDefaultVal](Attributable &attr) mutable {
278+
RecordType *record = dynamic_cast<RecordType *>(&attr);
279+
if (!record)
280+
{
281+
throw error::Internal("dynamic cast failure");
282+
}
283+
((*record).*setDefaultVal)(std::move(callDefaultValue)());
284+
};
260285
return *this;
261286
}
262287

0 commit comments

Comments
 (0)