mlpack / ensmallen

A header-only C++ library for numerical optimization --

Home Page:http://ensmallen.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C++20 redudant template argument disallowed in ctor

ElianeBriand opened this issue · comments

Issue description

If you don't support C++20, I guess this is not an issue but as a heads up, code like that does not compile with C++20:

template<typename MatType = arma::mat>
class EarlyStopAtMinLossType
{
 public:
  EarlyStopAtMinLossType<MatType>(...) :
  {  }

due to the following change that dissallow redundant template type declaration in the ctor and dtor declaration:

https://eel.is/c++draft/diff.cpp17.class#2

This is triggered by EarlyStopAtMinLossType by just including ensmallen.hpp, but possibly there other instances?

Fix is removing the <MatType> from the constructor and destructors.

Your environment

  • Tarball 2.17.0 from the website, also current git
  • Linux
  • g++ 11 with C++20 enabled
  • 10.7.1
  • CMake with set(CMAKE_CXX_STANDARD 20) in the cmakefile

Steps to reproduce

Compile with c++20 enabled, a cpp file that include ensmallen.hpp

Expected behavior

Compile.

Actual behavior

Does not compile

Fix

Confirmed that it is fixed by removing the redundant template type declaration to the ctor of EarlyStopAtMinLossType, resulting in:

namespace ens {

/**
 * Early stopping to terminate the optimization process early if the loss stops
 * decreasing.
 */
template<typename MatType = arma::mat>
class EarlyStopAtMinLossType
{
 public:
  /**
   * Set up the early stop at min loss class, which keeps track of the minimum
   * loss and stops the optimization process if the loss stops decreasing.
   *
   * @param patienceIn The number of epochs to wait after the minimum loss has
   *    been reached or no improvement has been made (Default: 10).
   */
  EarlyStopAtMinLossType(const size_t patienceIn = 10) :
      callbackUsed(false), 
      patience(patienceIn), 
      bestObjective(std::numeric_limits<double>::max()),
      steps(0)
  { /* Nothing to do here */ }

  /**
   * Set up the early stop at min loss class, which keeps track of the minimum
   * loss and stops the optimization process if the loss stops decreasing.
   *
   * @param func, callback to return immediate loss evaluated by the function
   * @param patienceIn The number of epochs to wait after the minimum loss has
   *    been reached or no improvement has been made (Default: 10).
   */
  EarlyStopAtMinLossType(
      std::function<double(const MatType&)> func,
      const size_t patienceIn = 10)
    : callbackUsed(true), 
      patience(patienceIn), 
      bestObjective(std::numeric_limits<double>::max()),
      steps(0), 
      localFunc(func)
  {
    // Nothing to do here
  }

@ElianeBriand Thank you for opening this issue, we would like to make ensmallen compatible with C++20. Would it be possible that you open a pull request to fix this issue?

Thanks for all the details, we have an ensmallen matrix build, but it looks like we don't test against C++20, I'll change the build as well. Let us know if you like to open a PR with the fix, happy to open one as well.

Once this is fixed we should aim to release ensmallen 2.18. Compared to the last release, the git repo has a bunch of other useful changes.

agreed