C++/Boost MPL: structure code likewise Haskell's let, where,
Asked Answered
A

2

9

As C++ metaprogramming is functional: is there any way of doing something comparable to any functional programming language's (e.g. Haskell's) let or where construct?

I'm using Boost::MPL but would like to have more structure for longer metafunctions. Splitting into several functions is fine but I'd prefer let/where in some cases.

Aromatize answered 27/6, 2012 at 8:39 Comment(6)
There comes a point where trying to shoehorn more template magic into C++ stops giving useful returns, especially when it comes to future compatibility and maintainance. Have you considered writing more code in a fast functional language which can generate libraries callable from C/C++? (OCaml springs to mind)Agripinaagrippa
No, I have not considered that. I'm using meta programming because I need to do something at compile time, not because I want to write functional code. My pieces of code are not that complex. I just thought if I used the same function application three times in a 30-line-metafunction it would be nice to use some structure.Aromatize
In C++11, you can do a lot of compile-time processing with constexpr instead of templates.Gaylordgaylussac
@Gaylordgaylussac I fail to see how that will help. Do you have an example where constexpr makes meta-programming more structured and allows let and where?Colincolinson
constexpr allows more structured code as it is procedural, not functional. I use it in some cases if only numeric expressions should be evaluated. In those cases, I would not use template meta-programming because the code gets a lot less readable and the benefit of faster code is too small (the compiler should care about!). E.g., I'm using boost::type_traits - I would not get that functionality using constexpr. So, constexpr is fine but cannot replace template metaprogramming in all situations (those, I'm searching a solution for).Aromatize
I'm completely clueless about C++, but let x = y in z is almost the same as (\x. z) y, so if anonymous functions are encodable maybe that's enough.Lashaunda
C
2

The MPL itself doesn't support let clauses but some of the libraries built on top of it do. One example is metamonad. As the name suggests it also supports some other higher-level functional concepts (monads). A big drawback is, that metamonad is not an official part of the Boost distribution.

As far as work-arounds for the MPL go, splitting things into functions and using more namespaces to group them and then import the important symbol into your top-level namespace is probably your best choice.

Colincolinson answered 27/6, 2012 at 10:7 Comment(1)
Thanks for pointing me to that library. Seems to be what I've been searching for altough I do not know yet, if I'll use it. Extra library, looks like magic,...Aromatize
A
1

Boost.Phoenix has a construct called let that allows you to declare local variables in a higher order function context. I think this is as high-level as it gets, though. It's still runtime, not compile-time.

From the linked page:

let(_x = 1, _y = ", World")
[
    // _x here is an int: 1

    let(_x = "Hello") // hides the outer _x
    [
        cout << _x << _y // prints "Hello, World"
    ]
]
Absorbance answered 27/6, 2012 at 14:19 Comment(1)
In my opinion, if you want to write functional code, you should take a functional language. I'm only search for a let-construct because template metaprogramming is functional.Aromatize

© 2022 - 2024 — McMap. All rights reserved.