# C++ equivalents of Fortran concepts

**URL:** <https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555>\
**Category:** Uncategorized\
**Created:** [May 25, 2022, 2:46pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555 "2022-05-25T14:46:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [May 25, 2022, 2:46pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/1 "2022-05-25T14:46:12Z")

</div>

I wish there were a guide to modern C++ for modern Fortran programmers that discussed analogous concepts. It could programmers moving in either direction. I don’t know C++ well, but here are a few thoughts. There was a previous thread [Fortran for C++ programmers ... guidelines?](https://fortran-lang.discourse.group/t/fortran-for-c-programmers-guidelines/193).

C++ now has modules, so you no longer need to write and #include header files.

C++ functions can declared [constexpr](https://iamsorush.com/posts/cpp-constexpr/), which remind me of **pure** functions in Fortran.

C++ functions can have **const** arguments, which can be **intent(in)** in Fortran.

C++ **auto** has type inference, similar to **associate** in Fortran.

C++ variables declared within braces have scope within those braces, similar to variables defined within **block, end block** in Fortran.

Here is a trivial C++ code using modules, followed by a Fortran analog.

math.cpp

```auto
export module math;

export constexpr int cube(const int n){
	return n*square(n);
}

// square() is not exported but can be used within module
constexpr int square(const int n){
	return n*n;
}

```

main.cpp

```auto
#include <iostream>
import math;

int main(){
	const auto n = 4;
	// line below invalid since square does not have export
	// attribute in module math
	// std::cout << "square of " << n << " is " << square(n) << "\n";
	std::cout << "cube of " << n << " is " << cube(n) << "\n";
}

```

compile with `g++ -fmodules-ts math.cpp main.cpp`

Fortran

```auto
module math
implicit none
private
public :: cube
contains
pure function cube(n)
integer, intent(in) :: n
integer :: cube
cube = n*square(n)
end function cube
!
! square() is not exported but can be used within module
pure function square(n)
integer, intent(in) :: n
integer :: square
square = n**2
end function square
end module math
!
program main
use math
implicit none
! line below invalid because square is not public in module math
! print*,"square of ",n," is",square(n)
associate (n => 4)
print*,"cube of ",n," is",cube(n)
end associate
end program main

```

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [May 25, 2022, 3:00pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/2 "2022-05-25T15:00:32Z")

</div>

> [@Beliavsky](#):
>
> C++ functions can declared [constexpr](https://iamsorush.com/posts/cpp-constexpr/), which remind me of **pure** functions in Fortran.

`constexpr` functions in C++ have particular semantics pertaining to compile-time computing for which there is **no analog** in Fortran in terms of nonintrinsic functions even though the standard includes certain intrinsic functions that can be consumed in constant expressions.

See this thread: there is great value in having the ability for a practitioner to author `CONSTEXPR` functions in Fortran that can yield rich dividends with scientific and technical computing. It will be helpful if more community members upvote the suggestion at the GitHub J3-Fortran site:

> <https://github.com/j3-fortran/fortran_proposals/issues/214#issue-936492046>
>
> "\*\*Constexpr as much as possible\*\*" or in other words, utilize as much compile-t…ime computing as viable is a mantra that's increasingly becoming important in many modern codes based on C++17 (and later revisions) toward scientific and technical computing in industry, especially in process simulations and modeling.
> 
> Improved \`constexpr\` in C++17 is particularly valuable when one needs to construct large named constants arrays for use in simulations based on other constants (e.g., coefficients in engineering correlations, results from prior simulation runs, results from first-principles models to be applied in reduced-order models, etc.). These are all use cases that are rather similar to some threads that have come up at Fortran-lang.org:
> \* \[User defined functions in constant expressions\](https://fortran-lang.discourse.group/t/user-defined-functions-in-constant-expressions/1509?u=fortranfan)
> \* \[Thread-safe initialization of shared module data (OpenMP)\](https://fortran-lang.discourse.group/t/thread-safe-initialization-of-shared-module-data-openmp/485?u=fortranfan)
> 
> An option can be to introduce a \*\*CONSTEXPR\*\* attribute to functions in Fortran combined with appropriate semantics and constraints which can allow such a nonintrinsic i.e., user-defined functions to be used in constant expressions.
> 
> Such an option appears consistent, in my view , with the evolution path of Fortran with \`PURE/ELEMENTAL\` procedures in Fortran 95 to \[\*\*SIMPLE\*\*\](https://j3-fortran.org/doc/year/19/19-201r1.txt) procedures in Fortran 202X.
> 
> The introduction of such a \*\*CONSTEXPR\*\* function in a future Fortran standard, say 202Y, will greatly enhance compile-time computing facilities in Fortran and it holds the promise of great benefit in scientific and technical computing.
> 
> Some further reading \[here\](https://fortran-lang.discourse.group/t/user-defined-functions-in-constant-expressions/1509/2?u=fortranfan)

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [May 25, 2022, 3:14pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/3 "2022-05-25T15:14:10Z")

</div>

I like the idea of juxtaposing the various programming constructs and concepts in this way, but my knowledge of C++ is too fragmentary and rudimentary to judge what the equivalents are. Just take the CONSTEXPR example - I would not have been able to pinpoint the difference as @FortranFan just did.

---

<div class="post-metadata">

**Author:** ![Pap](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pap/32/1305_2.png) [@Pap](https://fortran-lang.discourse.group/u/Pap)\
**Post date:** [May 25, 2022, 4:12pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/4 "2022-05-25T16:12:15Z")

</div>

Yes, C++ does have concepts that can be thought as analogous to Fortran’s. I still think a Fortran module is better designed though, and the ability to declare modules as `private` is implemented as cleanest as it gets. `constexpr` doesn’t sound a bad idea anyway, and I do think that C++ has a few (and I mean very few) features Fortran should also have - better class destructors comes to mind, even though such a thing is much less needed in Fortran than in C++.

But, expanding the topic, I wonder what is the analog of Fortran’s superb array support… there ain’t such a thing in C++. Yes, you finally have stuff like `std::vector<...>` and `std::array<...>` at your disposal, but you still have to “teach” them how to do any arithmetic by operator overloading and long templates that tend to compile very slowly. And even if you stomach that, I doubt you will ever end up with a full Fortran equivalent. I don’t know about you, but that alone should suffice to stick with Fortran. And I didn’t even mention complex arithmetic or even worse, complex arrays - well, I just did.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [May 25, 2022, 4:52pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/5 "2022-05-25T16:52:34Z")

</div>

> [@Pap](#):
>
> But, expanding the topic, I wonder what is the analog of Fortran’s superb array support… there ain’t such a thing in C++. Yes, you finally have stuff like `std::vector<...>` and `std::array<...>` at your disposal, but you still have to “teach” them how to do any arithmetic by operator overloading and long templates that tend to compile very slowly. And even if you stomach that, I doubt you will ever end up with a full Fortran equivalent. I don’t know about you, but that alone should suffice to stick with Fortran. And I didn’t even mention complex arithmetic or even worse, complex arrays - well, I just did.

The C++ experts don’t recommend using C-style multidimensional arrays. There are class libraries such as [Armadillo](http://arma.sourceforge.net/) and [Eigen](https://eigen.tuxfamily.org/index.php?title=Main_Page). In [Discovering Modern C++, 2nd. ed.](https://www.pearson.com/us/higher-education/program/Gottschling-Discovering-Modern-C-2nd-Edition/PGM2775899.html) Gottschling mentions [Blitz++](https://github.com/blitzpp/blitz), [MTL4](https://github.com/simunova/mtl4), and [HPR-BLAS](https://github.com/stillwater-sc/hpr-blas). I wonder what the risk is of using a C++ array library that stops being maintained.

---

<div class="post-metadata">

**Author:** ![mohoree](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mohoree](https://fortran-lang.discourse.group/u/mohoree)\
**Post date:** [May 26, 2022, 12:21am UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/6 "2022-05-26T00:21:08Z")

</div>

For arrays in C++, the following works well:

[https://www.math.ualberta.ca/~bowman/Array.html](https://www.math.ualberta.ca/~bowman/Array.html)

Interestingly, its’ aim is to be ‘as fast as Fortran’.

---

<div class="post-metadata">

**Author:** ![Pap](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pap/32/1305_2.png) [@Pap](https://fortran-lang.discourse.group/u/Pap)\
**Post date:** [May 26, 2022, 7:44am UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/7 "2022-05-26T07:44:31Z")

</div>

> [@Beliavsky](#):
>
> There are class libraries such as [Armadillo](http://arma.sourceforge.net/) and [Eigen](https://eigen.tuxfamily.org/index.php?title=Main_Page)

> [@mohoree](#):
>
> For arrays in C++, the following works well:  
> [John Bowman, Array](https://www.math.ualberta.ca/~bowman/Array.html)

I am aware there are libraries trying to address the problem, and I am sure you can find more of those mentioned. But does that really solve the problem? Relying on external software is not uncommon - we all do that, one way or another. But when you have to do it even for basic features such as arrays, then there is a problem. A big one.  
Everything is… matrices. You see them everywhere, you need them everywhere. Numerical Analysis is literally full of them. Graphics are full of them. Even game programming is full of them. It’s not like you have a uncommon feature that might be useful for a very specific task, so it’s ok if the language lacks it and you must rely on external libraries - which may or may not suit your needs, may or may not being maintained tomorrow. It’s basic stuff that should be there day one, at least for a modern high-level programming language.  
And I am not even talking about “sophisticated” constructs such as `where a>0; s=1; elsewhere; s=-1; end where`. One could argue this is a “luxury” they can live without (although I use it all the time.)

---

<div class="post-metadata">

**Author:** ![mohoree](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mohoree](https://fortran-lang.discourse.group/u/mohoree)\
**Post date:** [May 26, 2022, 9:02am UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/8 "2022-05-26T09:02:40Z")

</div>

I agree with you. I mentioned this because @Beliavsky’s list didn’t contain it. For use with the fftw and the other things that they have built (implicitly dealiased convolutions), this class works nicely. But, yes, this also doesn’t give me things like A(:,i) = 1.0\_dp (you have A.load(1.0)) that is trivial in Fortran.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [May 26, 2022, 7:26pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/9 "2022-05-26T19:26:15Z")

</div>

> [@Beliavsky](#):
>
> C++ functions can have **const** arguments, which can be **intent(in)** in Fortran.

For _ordinary variable_ dummy arguments in C++ (neither references nor pointers) the comparison does not make much sense as the modification of the dummy (passed by value) would not change the actual arg anyway.

It is ok for reference arguments. Mostly true for pointer arguments also, although

```auto
void sub(const int *i)
{
  const int j=6;
  *i = 8; // ILLEGAL, compiler error, as expected
  i = &j; // Perfectly LEGAL, no equivalence in Fortran
  printf("%d\n", *i);
}

```

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [May 26, 2022, 7:31pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/10 "2022-05-26T19:31:34Z")

</div>

> [@Beliavsky](#):
>
> I wonder what the risk is of using a C++ array library that stops being maintained.

There are plenty of Fortran libraries that have stopped being maintained, but we still use them. The C++ committee faces similar very problems as the Fortran one in terms of having to make compromises to remain (mostly) backward compatible. The build systems are the one which need maintenance if you ask me.

---

<div class="post-metadata">

**Author:** ![Niko](https://avatars.discourse-cdn.com/v4/letter/n/91b2a8/32.png) [@Niko](https://fortran-lang.discourse.group/u/Niko)\
**Post date:** [January 9, 2023, 2:45pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/11 "2023-01-09T14:45:01Z")

</div>

Not to open a new topic, I’ll ask it here: Do you know of some crash course of c++ for experienced Fortran programmers with basic knowledge of c? Ideally, it would be a list of equivalent simple codes in modern Fortran and c++. I have a bunch of routines in each of them that should work together. In total the code is not too long, so I’m trying to estimate what is cheaper, to look for the way to compile them together, or to translate the routines one way or another.

Many thanks!

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [January 9, 2023, 4:04pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/12 "2023-01-09T16:04:07Z")

</div>

> [@Niko](#):
>
> … Do you know of some crash course of c++ for experienced Fortran programmers with basic knowledge of c? …

@Niko, you may want to start by reviewing the information in this [**thread**](https://fortran-lang.discourse.group/t/fortran-for-c-programmers-guidelines/193).

A quick suggestion will be to start with a book (e.g., by Pohl) or a university guide from their computing department (many available online) that are unfortunately dated to late 1990s for C++ for Fortran programmers. Then move on immediately to modern C++ (C++20 and C++23) books and materials in conjunction with modern Fortran and kinda develop a guide of your own interest. The work on LFortran can be of help to you.

Ultimately the call is yours keeping in mind your colleagues and your computing domain(s), etc. as to whether you want to proceed with either all C++ codebase or a mixed-language C++ and Fortran codebase.  
Both are workable but the latter requires care , especially with C++ exception handling and with ensuring proper runtime and libraries that use C as the bridge layer. So many teams out there prefer the former. There are few circumstances now where it can be all Fortran.

---

<div class="post-metadata">

**Author:** ![cmaapic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/cmaapic/32/659_2.png) [@cmaapic](https://fortran-lang.discourse.group/u/cmaapic)\
**Post date:** [January 9, 2023, 5:23pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/13 "2023-01-09T17:23:19Z")

</div>

If all that you are interested in are solutions to a common set of problems in C++ and Fortran then the notes I’ve written may be of interest.

They are here

> **[cpp\_notes\_363.pdf](https://www.rhymneyconsulting.co.uk/cpp/cpp_notes_363.pdf)**
>
> 2.01 MB

I worked in Computing Services at King’s College, London and was responsible for the C++ training. Here are some of the people I provided training for.

Staff and research post graduates as part of the normal College training scheme;

MSc students in the Department of Mathematics doing an MSc in Financial  
Mathematics;

MSc students in Computer Science;

MSc students in Electrical Engineering;

Undergraduates in Mechanical Engineering as a component of a second year examinable course;

Research post graduates in the Mechatronics and Medicine Group at Imperial College in London;

Staff at Oxford University;

Staff at the Numerical Algorithms Group, Oxford;

and also indirectly whilst teaching on a number of C++ courses for PTR in Wokingham, including people from Qinetiq, Vertu (I had no idea that a mobile phone could cost £850,000!), AWE, Met Office,  
Virgin Media, RAF Waddington.

I keep the notes up to date, and the last set of changes was in November 2022.

YMMV

Ian Chivers

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [January 9, 2023, 7:33pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/14 "2023-01-09T19:33:40Z")

</div>

@cmaapic ,

Brilliant effort toward what is essentially a full-fledged book (perhaps you publish it as such?). A quick question: did you mean the title to be, “An Introduction to C++ Programming for Programmers”? Or, “… C++ Programming for Beginners”?

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [January 9, 2023, 11:34pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/15 "2023-01-09T23:34:20Z")

</div>

> [@Niko](#):
>
> Not to open a new topic, I’ll ask it here: Do you know of some crash course of c++ for experienced Fortran programmers with basic knowledge of c?
> 
> Many thanks!

A general question is to what C++ standard your C++ codes should adhere. I just bought the 3rd edition of Stroustrup’s [Tour of C++](https://www.amazon.com/Tour-C-Bjarne-Stroustrup-dp-0136816487/dp/0136816487) to see what has been added in recent standards (not that I have mastered the earlier ones). His book has been suggested for programmers coming to C++ from another language.

---

<div class="post-metadata">

**Author:** ![cmaapic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/cmaapic/32/659_2.png) [@cmaapic](https://fortran-lang.discourse.group/u/cmaapic)\
**Post date:** [January 10, 2023, 11:48am UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/16 "2023-01-10T11:48:08Z")

</div>

Most of the people today already have some programming background, so for programmers really.

---

<div class="post-metadata">

**Author:** ![Niko](https://avatars.discourse-cdn.com/v4/letter/n/91b2a8/32.png) [@Niko](https://fortran-lang.discourse.group/u/Niko)\
**Post date:** [January 12, 2023, 9:37am UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/17 "2023-01-12T09:37:41Z")

</div>

Thank you so much for pointing towards the other topic. I missed it completely. That’s exactly the information that I was looking for.

And thanks to the others for suggesting great sources! I got Stroustrup’s book and it looks very readable.

---

<div class="post-metadata">

**Author:** ![Niko](https://avatars.discourse-cdn.com/v4/letter/n/91b2a8/32.png) [@Niko](https://fortran-lang.discourse.group/u/Niko)\
**Post date:** [January 12, 2023, 9:42am UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/18 "2023-01-12T09:42:45Z")

</div>

I’m getting “Not found” message when I click on the link that you left. Could you please check if the file is there or my firewall is doing something weird?

---

<div class="post-metadata">

**Author:** ![cmaapic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/cmaapic/32/659_2.png) [@cmaapic](https://fortran-lang.discourse.group/u/cmaapic)\
**Post date:** [January 12, 2023, 12:34pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/19 "2023-01-12T12:34:12Z")

</div>

Many apologies. I’ve been having some difficulties configuring the site to make it accessible with  
better security protocols. I’ll do some more work on this today and make another post when I’ve tested  
the changes.

---

<div class="post-metadata">

**Author:** ![cmaapic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/cmaapic/32/659_2.png) [@cmaapic](https://fortran-lang.discourse.group/u/cmaapic)\
**Post date:** [January 12, 2023, 3:55pm UTC](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555/20 "2023-01-12T15:55:19Z")

</div>

I’ve altered some of the security settings. Would you like to try now?

[Next page](https://fortran-lang.discourse.group/t/c-equivalents-of-fortran-concepts/3555.md?page=2)
