QuantLib icon indicating copy to clipboard operation
QuantLib copied to clipboard

error: default initialization of an object of const type 'const ext::function<Real (Real)>' (aka 'const function<double (double)>') without a user-provided default constructor

Open ryandesign opened this issue 4 years ago • 6 comments

Hello, QuantLib 1.22 fails to build on OS X 10.11.6 using the compiler Apple LLVM version 8.0.0 (clang-800.0.42.1) that comes with Xcode 8.2.1:

nthorderderivativeop.cpp:59:41: error: default initialization of an object of const type 'const ext::function<Real (Real)>' (aka 'const function<double (double)>') without a user-provided default constructor
        const ext::function<Real(Real)> emptyFct;
                                        ^
                                                {}

Here's a full build log from the MacPorts buildbot system.

QuantLib claims to only require C++11, and Apple clang 500.2.75 and later are supposed to support C++11, and I am manually adding -std=c++11 to CXXFLAGS to turn C++11 mode on (since the build system doesn't do it automatically), so I would expect this to work.

Building on OS X 10.11.6 with open source clang 9.0.1 as installed by MacPorts works.

Building on macOS 10.12.6 with Apple LLVM version 9.0.0 (clang-900.0.39.2) from Xcode 9.2 works, as does Apple clang from newer Xcodes on newer macOS versions.

I'm not familiar with modern C++ so I wanted to report this in case maybe you are accidentally using C++ features that are from a later standard than C++11, or if this is a compiler bug then maybe there is something you could do to work around it.

ryandesign avatar Apr 26 '21 13:04 ryandesign

I'm a bit wary of saying it's a compiler problem, but it looks like it. May you try and see if this workaround fixes the issue?

diff --git a/ql/methods/finitedifferences/operators/nthorderderivativeop.cpp b/ql/methods/finitedifferences/operators/nthorderderivativeop.cpp
index b15eeeeba..a61e6e153 100644
--- a/ql/methods/finitedifferences/operators/nthorderderivativeop.cpp
+++ b/ql/methods/finitedifferences/operators/nthorderderivativeop.cpp
@@ -56,7 +56,7 @@ namespace QuantLib {
              "inconsistent number of points");
 
         Array xOffsets(nPoints);
-        const ext::function<Real(Real)> emptyFct;
+        ext::function<Real(Real)> emptyFct;
 
         for (FdmLinearOpIterator iter = layout->begin(); iter!=endIter; ++iter) {
             const auto ix = Integer(iter.coordinates()[direction]);

lballabio avatar Apr 26 '21 14:04 lballabio

This issue was automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment, or this will be closed in two weeks.

github-actions[bot] avatar Jun 26 '21 02:06 github-actions[bot]

May you try and see if this workaround fixes the issue?

That does get past that error. The next error I see is:

fdmtimedepdirichletboundary.cpp:33:34: error: constructor for 'QuantLib::FdmTimeDepDirichletBoundary' must explicitly initialize the const member
'valuesOnBoundary_'
    FdmTimeDepDirichletBoundary::FdmTimeDepDirichletBoundary(
                                 ^
../../../../ql/methods/finitedifferences/utilities/fdmtimedepdirichletboundary.hpp:63:54: note: 'valuesOnBoundary_' declared here
        const ext::function<Disposable<Array>(Real)> valuesOnBoundary_;
                                                     ^
fdmtimedepdirichletboundary.cpp:41:34: error: constructor for 'QuantLib::FdmTimeDepDirichletBoundary' must explicitly initialize the const member
'valueOnBoundary_'
    FdmTimeDepDirichletBoundary::FdmTimeDepDirichletBoundary(
                                 ^
../../../../ql/methods/finitedifferences/utilities/fdmtimedepdirichletboundary.hpp:62:42: note: 'valueOnBoundary_' declared here
        const ext::function<Real (Real)> valueOnBoundary_;
                                         ^
2 errors generated.

ryandesign avatar Jun 26 '21 04:06 ryandesign

Try removing the const from the two declarations listed (ql/methods/finitedifferences/utilities/fdmtimedepdirichletboundary.hpp, lines 62 and 63.)

lballabio avatar Jun 26 '21 14:06 lballabio

The next error then is:

numericaldifferentiation.cpp:72:37: error: default initialization of an object of const type 'const ext::function<Real (Real)>' (aka 'const function<double (double)>') without a user-provided default constructor
    const ext::function<Real(Real)> f;
                                    ^
                                     {}
numericaldifferentiation.cpp:102:37: error: default initialization of an object of const type 'const ext::function<Real (Real)>' (aka 'const function<double (double)>') without a user-provided default constructor
    const ext::function<Real(Real)> f;
                                    ^
                                     {}
numericaldifferentiation.cpp:129:37: error: default initialization of an object of const type 'const ext::function<Real (Real)>' (aka 'const function<double (double)>') without a user-provided default constructor
    const ext::function<Real(Real)> f;
                                    ^
                                     {}
numericaldifferentiation.cpp:156:37: error: default initialization of an object of const type 'const ext::function<Real (Real)>' (aka 'const function<double (double)>') without a user-provided default constructor
    const ext::function<Real(Real)> f;
                                    ^
                                     {}
numericaldifferentiation.cpp:175:37: error: default initialization of an object of const type 'const ext::function<Real (Real)>' (aka 'const function<double (double)>') without a user-provided default constructor
    const ext::function<Real(Real)> f;
                                    ^
                                     {}
numericaldifferentiation.cpp:279:37: error: default initialization of an object of const type 'const ext::function<Real (Real)>' (aka 'const function<double (double)>') without a user-provided default constructor
    const ext::function<Real(Real)> f;
                                    ^
                                     {}
6 errors generated.

If I remove these consts in test-suite/numericaldifferentiation.cpp, then the build succeeds.

It sounds like the compiler is suggesting that inserting {} where indicated would fix this. If I leave the const and add the {} where shown, that build also succeeds.

ryandesign avatar Jun 27 '21 07:06 ryandesign

Thanks! Leaving the const and adding the {} is probably better. I'd try that with the previous errors, as well.

Once it works, may you create a pull request with the changes?

lballabio avatar Jun 28 '21 07:06 lballabio