LLNL / sundials

Official development repository for SUNDIALS - a SUite of Nonlinear and DIfferential/ALgebraic equation Solvers. Pull requests are welcome for bug fixes and minor changes.
https://computing.llnl.gov/projects/sundials
BSD 3-Clause "New" or "Revised" License
520 stars 129 forks source link

Providing default constructors for structs breaks use of designated initializers #329

Closed ianabel closed 8 months ago

ianabel commented 1 year ago

Using Sundials in a C++20 program results in no clean way to instantiate custom SUNLinearSolvers (and other obejcts).

The usual

struct _generic_SUNLinearSolver_Ops LSOps =·
{
.gettype = SunLinSolWrapper::LSGetType,
.getid = SunLinSolWrapper::LSGetID,
.setatimes = nullptr, ...
};

Isn't permitted because the default constructor makes _generic_SUNLinearSolver_Ops a non-aggregate type.

From include/sundials/sundials_linearsolver.h:127

#ifdef __cplusplus
  _generic_SUNLinearSolver_Ops() = default;
#endif

Removing this default constructor results in a pure aggregate type and declaring the default constructor should not be necessary.

I know this is low priority, but flagging this as usage of C++20 will only increase.

ianabel commented 1 year ago

This appears to have been added in the Ginko interface commit 86996b3 last october by @balos1 -- was it needed to get ginko working?

drreynolds commented 1 year ago

Thanks for bringing this up @ianabel. @balos1 is on travel this week, but hopefully he'll chime in soon.

balos1 commented 1 year ago

For reference, this issue occurs with C++20 because C++20 changed the definition of an aggregate so that it cannot have any constructor, while previously a default constructor was acceptable.

ianabel commented 1 year ago

Right, I guess the question is -- was the default constructor needed for something specific.

By my reading the C++ standard defines the behaviour of constructing an aggregate to be exactly what the default constructor would have done (in this case, with simple types). So it should be fine to just remove, but if there's a particular reason it was there I'm happy to debug whether removing it is an issue.

balos1 commented 1 year ago

I cannot recall exactly why the default constructor was added. Removing it does not seem to break anything in my limited testing so far. If you are able to try it out as well, that would be great. Here is the branch where I removed it https://github.com/LLNL/sundials/tree/bugfix/cpp20-aggregate.