Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Compilation issue with custom state type with clang only

Open
#48 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
compilers

Research direction

Start with the linked Godbolt reproducer and compare clang++ with the compilers that accept it. Inspect the boost/numeric/odeint.hpp inclusion path and the vector_space_algebra use with std::pair; done means the shown program compiles with clang++ without moving the operator definitions before the odeint include.

Written by the indexing model from the issue text.

Description

In the code below, I am working with a custom state-type (a std::pair in this case). The code compiles with g++, icc, msvc. With clang however, compilation fails. In order to get it to build successfully, I apparently need to define operator+() and operator*() for the state type before including the odeint header. Link to godbolt: https://godbolt.org/z/6eavzdxbf

#include <cstdlib>
#include <utility>

#include <boost/numeric/odeint.hpp> // if included here, compilations fails with clang++

using state_type = std::pair<double, double>;
using deriv_type = state_type;
using time_type = double;

state_type operator+(state_type a, state_type const& b);
state_type operator*(state_type a, time_type b);
state_type operator*(time_type b, state_type a);

//~ #include <boost/numeric/odeint.hpp> // if included here, no error!

state_type operator+(state_type a, state_type const& b) {
	a.first += b.first;
	a.second += b.second;
	return a;
}

state_type operator*(state_type a, time_type b) {
	a.first *= b;
	a.second *= b;
	return a;
}

state_type operator*(time_type b, state_type a) {
	return a * b;
}

void harmonic_oscillator(const state_type& x, deriv_type& dxdt, time_type) {
	static auto constexpr gam = 0.1;

	dxdt.first = x.second;
	dxdt.second = -x.first - gam*x.second;
}

namespace bno = boost::numeric::odeint;
using stepper_type = bno::runge_kutta_dopri5<state_type, double, deriv_type, time_type, bno::vector_space_algebra>;

int main() {
	state_type x0{1.0, 0.0};
	
	// just to see if the operators work...
	x0 * 1.;
	2. * x0;
	x0 + x0;
	
	stepper_type stepper;
	time_type const dt = 0.005;
	for (time_type t = 0; t < 10; t += dt) {
		stepper.do_step(harmonic_oscillator, x0, t, dt);
		std::cout << t << '\t' << x0.first << '\t' << x0.second << '\n';
	}
	
	return EXIT_SUCCESS;
}
Dominant language
C++
Stars
55
Forks
59
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/odeint

All issues in boostorg/odeint

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.