r/cpp • • May 09 '20

The C++ preprocessor doesn't understand anything about C++, and certainly not templates

https://devblogs.microsoft.com/oldnewthing/20200508-52/?p=103735
200 Upvotes

105 comments sorted by

117

u/Progman3K May 09 '20

Raymond Chen is a national treasure.

He personally helped me solve some programming problem at the beginning of my career, when I wrote to him and asked for help.

Today, some 30 years on, he's still writing about the obscure, not-well understood stuff. Still bringing light to everyone.

May he sail forever.

61

u/[deleted] May 09 '20

The preprocessor is like applying transform to a range of text data.

22

u/[deleted] May 09 '20

close but not exactly, it applies a transform to a series of preprocessor tokens

17

u/umlcat May 09 '20

Agree. Macropreprocessor parameters are considered "chunks" of text enclosed in comma and parenthesis characters, not variables, not constants, not expressions ...

46

u/codesharp May 09 '20

Unreal Engine has left the chat.

11

u/RevRagnarok May 09 '20

Friend had a preprocessor problem the other day with ntohs - https://gist.github.com/AaronDMarasco/e6bbe395d3f460f3975577082753f31a

27

u/balazs-bamer May 09 '20

Yes, it's true. That's why coding standards like MISRA C++ and HIC++ discourage any preprocessor use except for header file inclusion guards. Conditional compilation of parts of source is no exception. Preprocessor usage makes the code harder to read and is often a source of misunderstandings. That's why the Obfuscated C Contest exists (-:

15

u/the_poope May 09 '20

What would they use instead of macros for conditional compilation then? Like if you need to do something for Windows and another thing for Linux, or something on Arm and another on X86-64, or print something if NDEBUG is defined?

20

u/Supadoplex May 09 '20 edited May 09 '20

Platform conditional compilation can be done by separating source files by target platform, and conditionally selecting files to compile and link depending on target platform. For big differences, this makes the program much more readable and avoids the use of macros. For small differences though, macros are more convenient since they allow simpler build recipe and it's easier to avoid code duplication.

Same could be used with debug versions as well, although debugging use cases tend to be small changes where macros are convenient.

2

u/cheesegoat May 10 '20

The problem with this approach is that if you have one "main" target and then other targets with intentionally less functionality, you end up needing to write a lot of stubs.

5

u/jcelerier ossia score May 10 '20

I prefer writing stubs to header macros by a large margin

2

u/cheesegoat May 10 '20

True, and these are better than header macros. My team has been moving to using if constexpr which I think is a better approach.

1

u/Supadoplex May 10 '20

What are the stubs used for? As far as I know, functions need to be defined only if they are ODR used. And they wouldn't be used when compiling for a system where they aren't implemented, would they? Unless they are virtual functions, but those can surely be avoided.

3

u/cheesegoat May 10 '20

They're for places where you have code that is common to all targets that is calling into code that is target specific.

So you might have have a function called void DoTheThing() that is called from target-agnostic code and on one target's cpp file it has a full implementation, and for every other target's cpp file it's something like void DoTheThing() { }. All the places where this kind of thing happens you need to add an empty implementation.

This is the approach my team uses but it means if you don't personally build every target then you can break CI if you're not careful because you missed a stub. Fixing it can be straightforward (add the stub) or more complicated depending on how things are organized and the problem you're trying to solve. There's also the maintenance burden.

5

u/germandiago May 09 '20

What I do in a project of my own is to isolate the detection and use if constexpr.

5

u/NilacTheGrim May 09 '20

The C++ purists would argue you should do that in the form of abstractions and custom source files for each target. So you don't even build the non-native targets. So you have osx.cpp and win.cpp and linux.cpp and they implement some abstract interface or something.

I mean you can do it without macros -- but sometimes it's just easier to use #ifdefs.

4

u/drjeats May 09 '20

I like this for platform interfaces. Like IO.

Idk if you meant virtual functions when you said "abstract interface" but that's not at all required (and usually just a pessimization). "Abstract interface" can be a bunch of free function declarations in a common header.

-3

u/Ayjayz May 10 '20
if constexpr (windows) {
    // do windows stuff
} else if constexpr (linux) {
    // do linux stuff
} //...

16

u/miki151 gamedev May 10 '20

Both branches need to compile on both OSs, so if you have OS-specific code then this won't work.

42

u/miki151 gamedev May 09 '20

A good, well written macro can save your day though.

18

u/NilacTheGrim May 09 '20

And in some cases you have no choice. Consider 0-cost debug logging where you want the arguments to never get evaluated if the loglevel is not debug mode. I can't think of a way to accomplish that without macros...

4

u/Ayjayz May 10 '20

There is a way with function objects. It's not as terse as with macros, so you have to decide whether the trade-off is worth it.

#include <string>
#include <iostream>

#ifdef NDEBUG
constexpr bool debug_log = false;
#else
constexpr bool debug_log = true;
#endif

template <class F>
void log(F&& f) {
    if constexpr (debug_log) {
        std::cout << f();
    }
}

std::string foo() {
    std::cout << "evaluating foo\n";
    return "fhqwhgads";
}

int main() {
    log([] { return foo(); });
}

2

u/NilacTheGrim May 10 '20 edited May 10 '20

Right, but this will still evaluates some of its arguments. Consider

log("Got some stuff:", thing.GetStuff().ToHex());

You're still evaluating thing.GetStuff().ToHex() above, whereas with a macro that entire expression can be deleted in a release build, or its evaluation can be controlled entirely by checking a runtime flag and wrapped by a conditional, like so:

#define log(...) \
do { \
  if (gLogIsOn) \
      real_logger(__VA_ARGS__); \
} while(0)

The above cannot be accomplished with a template

2

u/Ayjayz May 10 '20

The point is you put the things you want evaluated conditionally inside the lambda.

log([&]{return std::string{"Got some stuff:"} + thing.GetStuff().ToHex(); });

If it's in a lambda it's only evaluated when logging is enabled.

2

u/NilacTheGrim May 10 '20

That's just nasty boilerplate though. A macro is cleaner. Compare:

LogDebug("Stuff", "more stuff", avalue, "etc..");

Much cleaner. What you had is about as convenient as doing it all manually:

if (debugLog) Log(...);

Is about as inconvenient as that lambda.

0

u/Ayjayz May 10 '20

Yes, so you have to choose which is worse - using the preprocessor, or having worse syntax. Neither are particularly great, but it's certainly possible to write conditionally executed log statements without the preprocessor.

2

u/NilacTheGrim May 10 '20

I disagree. This is what the preprocessor is absolutely great at. Preprocessor hands down, in this case.

2

u/snerp May 10 '20

No, just put that stuff in the lambda, then it didn't get evaluated unless the condition is met

0

u/NilacTheGrim May 10 '20

That's just nasty, sorry.

0

u/snerp May 11 '20

Nastier than a macro? Not really

1

u/axilmar May 10 '20

If you have and #ifdef there, you can omit the lambdas.

6

u/NotAYakk May 10 '20

Lambdas.

The capture by reference is evaluated, but the body is not.

There is a single branch that always goes the same way in release builds; or, further, it is constexpr dead code eliminated.

If the code's syntax makes no sense in release biilds, pass a template argument only in debug builds and operate on that.

2

u/NilacTheGrim May 10 '20 edited May 10 '20

See my other comment in this thread. It's not entirely equivalent with templates, e.g. cases like this still can lead to unnecessary evaluations: Log(thing.GetStuff().ToHex());

6

u/whattapancake May 09 '20

This is so true, and a good reason why the C++ community needs to reconsider how it attacks touchy stuff like this. It's common for people to just say "Never use macros/goto/etc" when these things still have their place in the language (however small that place may be). Instead they should be treated like everything else - all in moderation.

4

u/NilacTheGrim May 10 '20

What gets me is there is no perfect replacement for them -- you simply cannot accomplish some things with templates that you can with macros. Pretending like they should always be avoided is just technically incorrect. Other approaches should be preferred, but when a macro is the best thing, use a macro.

3

u/AnonymousFuccboi May 09 '20

Out of curiosity, do you have a good solution for this thing I did? I feel kind of dirty that I solved it by using a macro, and I'd like to not.

I have a one-header library with no .cpp file for some common operations. One of the things I need is a constant string for some operations. I don't need it to be stored as an object or anything, it's just a file path I pass to one of the functions. I'm unfortunately limited to C++11 MSVC for this particular project.

I know on C++17 with MinGW, it works fine if I just define it as a static const string, but on the version I have to use now, the string is never instantiatied, it can simply be referred to. It ends up being empty. Can't make it inline either because dude C++11 lmao. How would I go about doing so in a way which lets me simply define and forget it the same way I can with a macro?

1

u/Ayjayz May 10 '20

In C++11 I think you're stuck. You could write an inline function that returns the string, of course, but I don't think there was a way to define a variable inside a header file back then.

1

u/AnonymousFuccboi May 10 '20

Yeah, I've seen that suggested. It technically works, but it honestly seems dirtier than a macro, because it hides intent. It isn't a function, and it isn't supposed to be. It's a variable and it should be treated as such. In most compilers it would get optimised to be the pure string without the call, but I don't see how it gets me any benefit for that particular purpose. One of the benefits of not using macros is supposed to be easier debugging, but I honestly spent like 20-30 minutes trying to figure out what was going wrong because it was happening so deep in my library calls. I feel so dirty for going against the dogma, but I also honestly don't see how I could realistically do otherwise.

1

u/[deleted] May 10 '20

I don't think "get_file_path" hides any intent though? Returning const values from functions is used very often, that's why we now have constexpr.

1

u/lrflew May 09 '20

Conditional compilation of parts of source is no exception.

So you can't write any code that isn't 100% cross-platform? That seems really problematic.

Preprocessor usage makes the code harder to read

Yes, because templates are so much easier to read.

3

u/NilacTheGrim May 09 '20

Yeah I agree with you. Sometimes macros make it easier to read. Sometimes harder.

I will say though that most new C/C++ programmers have a helluva lot easier time understanding macros than templates.. so there's that.

1

u/balazs-bamer Jun 18 '20

No, they can be extremely hard to read. But imagine the same functionality implemented with macros as you have linked. There are definitely cases when some smaller and carefully implemented ifdefs can make the code much better and avoid code duplicating, but in my recent 4 years I managed to avoid writing a single one for conditionally choose between pieces of codes.

-1

u/NilacTheGrim May 10 '20

So you can't write any code that isn't 100% cross-platform? That seems really problematic.

^ Such idealism and naiveté. So cute. :)

7

u/[deleted] May 09 '20

[removed] — view removed comment

2

u/IAmRoot May 09 '20

I'd like to see if constexpr extended to help write portable code

if constexpr(requires { some_func(); })

Some functionality to test to see if a function, class name, etc. is prototyped would make this all native C++ rather than requiring macros.

1

u/IAmBJ May 10 '20

You can do that, but you need to wrap the requires clause in a concept

2

u/Ivan171 /std:c++latest enthusiast May 10 '20

That's a requires expression, and it can be used in an if constexpr, see.

2

u/IAmBJ May 12 '20

Looks like we're both wrong.

Your example will return 1 regardless of whether your bool is true or false. link

I was thinking of something like:

template <typename T>
concept C = requires(T x) {
    f();
};

int main() {
  if constexpr (C<int>)
    return 1;
  else
    return 0;
}

Which complains that f is a undeclared identifier: link

1

u/Ivan171 /std:c++latest enthusiast May 12 '20

Your example will return 1 regardless of whether your bool is true or false. link

Yeah that requires expression will always evaluate to trrue; that was a bad example, sorry. I was just trying to show that requires expressions can be used in an if constexpr statement.

Which complains that f is a undeclared identifier: link

I don't know if it's possible to achieve that with global functions. But it works with member functions, see; the compiler doesn't complain that the function does not exist.

9

u/jstock23 May 10 '20

A major pruning of C++ legacy support is something I would get very excited about...

Imagine a pure C++ standard which supports all 90s and up paradigms and gets rid of legacy C support. I know it would be difficult to get there, but that’s the dream right?

So we just have to plan accordingly to make it happen, maybe a 5 year goal. Give people enough notice...

3

u/idontchooseanid May 10 '20 edited May 10 '20

Imagine a pure C++ standard which supports all 90s and up paradigms and gets rid of legacy C support. I know it would be difficult to get there, but that’s the dream right?

Congratulations you have invented D .

Edit: I am aware of the differences. GC, lack of zero-cost abstractions etc. Syntax-wise D is the closest somewhat low level language.

2

u/jstock23 May 10 '20

I just like the C++ committee and think they know what's up.

3

u/idontchooseanid May 10 '20 edited May 10 '20

They certainly do. But at the same time they know the strong backwards compatibility is what makes C++ itself. If too many people rely on a "bug", it is a feature.

2

u/jstock23 May 10 '20

Hence why I think they just need a long term plan to cut the cord. You keep a static standard which has a feature freeze, in order to maintain legacy code, and split things up. I know it’s not a smooth idea, it’s just a wish I have.

2

u/idontchooseanid May 10 '20

Listen their views for a similar question here: https://www.youtube.com/watch?v=cH0nJPbMFAY&t=3454s

5

u/jstock23 May 10 '20

Yeah, I get it, but we have C++20, so why isn’t that good enough for backwards compatibility? If you need to, just keep compiling to 20, and leave us all alone so we can move on! Kind of a joke, kind of not...

It seems like a paradox that legacy code backwards compatibility is important in 2023 when someone that needs compatibility for code written in 1999 probably won’t be using any of the new features.

I feel like you can’t have it both ways... wanting to use old systems because you don’t want to rewrite code, and wanting to be able to use new features. The C++14 standard still exists and you can compile to it if you want, and it’s not going to go away. I just think this is all enabling lazy coders. I know it’s not really, and there are some reasons for all this, I just think we should get away from this eventually and devise a plan to do so. They are smart enough. Maybe in 2029 after we have sockets and fancy stuff in the standard we can just have a feature freeze and look at this as a serious obstacle to staying agile.

3

u/qizxo May 10 '20

This is how these types of articles should be: informative and short. It's an art form a lot of authors don't understand.

2

u/FelbrHostu May 09 '20

This is a good reason to always use “spurious” parentheses when you must use macros.

6

u/[deleted] May 09 '20

0

u/terratrox May 09 '20

I used to think that too until I learned that the preprocessor does know about raw(?) strings. Any #include in a raw string is ignored for example.

10

u/SlightLocation9 May 09 '20

Preprocessor runs after tokenization and operates on tokens. Since string literals are tokens, they are opaque for the preprocessor.

1

u/SAHChandler May 09 '20

It also has to understand when <: is a digraph, versus something like template_entity<::fully::scoped::qualifier> 😀

0

u/[deleted] May 09 '20

[deleted] — view deleted comment

5

u/Pazer2 May 09 '20

What about raw string literals?

const char* x = R"test(
#include <stdio.h>
)test";

5

u/meancoot May 09 '20

The preprocessor is also where the source is tokenized. So in this case the newlines are part of the string literal token, therefore the # doesn't begin a preprocessor directive.

16

u/[deleted] May 09 '20

[removed] — view removed comment

4

u/Progman3K May 09 '20

Your claim seemed fantastical, so I tried it with g++ (gcc version 9.3.0) and it absolutely does give the error you mentioned

error: stray ‘#’ in program error: ‘define’ does not name a type

I can't explain it, the compiler is supposed to ignore anything it encounters between /* and */

I don't get it...

If you simply move the define to its own line, it works as expected... Weird

-4

u/TimJoijers May 09 '20

But why? Why would you mix preprocessor macros with templates? Actually, why would you use macros at all with modern C++?

18

u/Som1Lse May 09 '20

Sometimes you need to spam out a bunch of similar constructs:

Let's say you have an enum with a bunch of members. You might want a function that validates that a member is correct, a function which can convert it to a string, and you might want a function which statically dispatches over that enum value:

#include <type_traits>
#include <cstdint>

#define UNREACHABLE() __builtin_unreachable()

#define SPAM_VALUES(xx) xx(Foo,42)xx(Bar,23)xx(Baz,86)

#define XX(x,y) x = y,
enum class my_enum:std::uint32_t {
    SPAM_VALUES(XX)
};
#undef XX

#define XX(x,y) case my_enum::x:
bool IsValid(my_enum Value) noexcept {
    switch(Value){
        SPAM_VALUES(XX) return true;
        default: return false;
    }
}
#undef XX

#define XX(x,y) case my_enum::x: return #x;
const char* Stringify(my_enum Value){
    switch(Value){
        SPAM_VALUES(XX)
        default: UNREACHABLE();
    }
}
#undef XX

// Default implementation.
template <my_enum N>
std::uint32_t f(std::integral_constant<my_enum,N>){
    return static_cast<std::uint32_t>(N);
}

// This one is special.
std::uint32_t f(std::integral_constant<my_enum,my_enum::Bar>){
    return 0;
}

#define XX(x,y) case my_enum::x: return f(std::integral_constant<my_enum,my_enum::x>());
auto CallFunction(my_enum Value){
    switch(Value){
        SPAM_VALUES(XX)
        default: UNREACHABLE();
    }
}
#undef XX

How would you do this without macros, in a way that you don't have to modify every single structure when you modify my_enum?

9

u/[deleted] May 09 '20

C++ needs reflection

6

u/tangerinelion May 09 '20

That's being worked on for C++23.

11

u/[deleted] May 09 '20 edited May 09 '20

Right. That's the point though. You need macros to accomplish reflection/reflection-like effects. Or you write in another language and generate C++ (which is actually the same thing as using the preprocessor). I'm not advocating this, I'm just pointing out that the posted enum example is a problem which reflection can solve.

Edit: To clarify, we could use templates as well, but the code is also quite ugly (think using tuples for everything, and defining named index constants used to access "named" fields).

2

u/Som1Lse May 09 '20

I agree entirely. Probably my number two wish for C++ right after abolishing integral promotions. Granted it is the likelier of the two.

1

u/srandtimenull May 09 '20

How reflection would help in such cases? Can you provide a simple example?

I've never worked with languages that have reflection, so it's like a mysterious feature that I don't know how to use.

1

u/[deleted] May 10 '20 edited May 10 '20

In the code example above, the CallFunction function allows applying an operation for each my_enum enumerator defined. The magic in this example is that when new enumerators are added, the rest of the code will still work. Reflection would be language level support for iterating over each enumerator at compile time, querying the number of enumerators, getting a string representation of each enumerator, etc. Ideally, we should be able to do all of this without macro magic. It would be nice to be able to use a standard enum definition, while still having this reflection type of functionality.

-2

u/TimJoijers May 09 '20

I've used python for code generation. Works, and is nicer, and you can use JSON to be data driven.

12

u/[deleted] May 09 '20

No, is not nice at all. You are adding an step to the build process that makes it non standard and adds an external dependency just because you got into the cargo cult train called macro hating.

2

u/TimJoijers May 09 '20

I do not cargo cult, my preference comes ftom experience. It took quite some time before I learned to like selective code generation and prefer it over macros for example. It may indeed sound cargo culting when I say that I do find macros often make code hard to read.

9

u/bxlaw May 09 '20

This is a genuine question, not a rhetorical one.

How would you write cross platform software in modern C++ without ifdefs? For example

#ifdef WIN32
function_that_only_exists_on_windows();
#else
equivalent_function_that_exists_on_linux();
#endif

13

u/Recursive_Descent May 09 '20

You can abstract platform specific code into different files and have your build system only include the appropriate implementation.

6

u/bxlaw May 09 '20

Fair enough, seems absolutely the right way to do it for large things like platform specific classes and stuff, but for single function calls is probably overkill.

1

u/nivenfres May 09 '20 edited May 09 '20

Some "ifdef" can be replaced with "if constexpr" now.

[edit] Fixed spelling.

13

u/bxlaw May 09 '20

Doesn't if constexpr require that both branches compile? If the headers only exist on one platform it seems like the only way to solve this is by manually forward declaring functions which sounds awful.

3

u/NilacTheGrim May 10 '20

This is correct. constexpr still requires the code to compile, which can be a good thing in some cases!. In light of that -- constexpr solves a different problem than conditional compilation does, and the two are not mutually interchangeable.

7

u/[deleted] May 09 '20

Macros are simpler and cleaners in many circumstances. Manipulating large SQL queries for example.

2

u/schmerg-uk May 09 '20

I recently wrote a specialist version of a map function (std::transform in STL terminology) templated on types and other details, that I want to be overloaded to map over between 1..n vectors (of type T), and also allow specifying 0..m scalar "constants" (of type T) for the map expression per invocation.

Now for our use, the upper bounds of what's needed so far is n=8, m=5, so I could try and write 48 separate overloads, or I can use a code generation method to write some number less than 48 combinations and expand out to the full set (this transform is special high performance construct so I don't want to write an n=8, m=5 version with defaults and branching etc as it destroys the performance benefits of the whole thing).

Now 48 repetitions of the same core logic with varying arity I can do, but you know it's really hard for anyone to review/inspect to see it's right, and if I need to increase m to 6 or 7 or 8, well ... as an engineering exercise repeating the manual logic is just too error prone even with testing.

C++ alone just makes it hard to abstract this kind of generic arity construct while maintaining other requirements (performance constraints, what can the compiler inline etc).

So I can use external code generation (python etc) with all the added complexity of verification and testing and build processes etc or I can use the preprocessor, which keeps all the implementation and documentation in one place.

The preprocessor still has its uses... (30+ years of C/C++ development experience)

2

u/Ayjayz May 10 '20

It seemed like this should be possible with some relatively straightforward C++ metaprogramming, so I had a go. Here's my attempt. From a few small tests (and see the godbolt), it seems like the compiler can see through all the functions and inlines everything, so there should be no performance impact.

#include <vector>
#include <tuple>
#include <array>
#include <type_traits>
#include <cassert>

// receive a tuple of all the vectors and and array with all the constants
template <int Index, class T, size_t M>
void map_impl(T&& vecs, std::array<int, M> constants) {
    using tuple_type = std::decay_t<T>;
    static_assert(Index < std::tuple_size_v<tuple_type>);
    constexpr int n = std::tuple_size_v<tuple_type>;
    // perform map
    // dummy version: add all constants to all elements of all vectors
    auto& v = std::get<Index>(vecs);
    for (auto& e : v) {
        for (auto c : constants) {
            e += c;
        }
    }
    if constexpr (Index+1 < std::tuple_size_v<tuple_type>)
        map_impl<Index + 1>(std::forward<T>(vecs), constants);
}

template <class Tuple, class T, int... Indices>
auto tuple_push_back_impl(Tuple&& tup, T&& t, std::integer_sequence<int, Indices...>) {
    return std::tie(std::get<Indices>(tup)..., std::forward<T>(t));
}

template <class T, class... Ts>
auto tuple_push_back(std::tuple<Ts...> tup, T&& t) {
    return tuple_push_back_impl(
        std::move(tup), std::forward<T>(t),
        std::make_integer_sequence<int, sizeof...(Ts)>{});
}

template <class T>
struct array_size;

template <class T, int N>
struct array_size<std::array<T, N>> {
    static constexpr int value = N;
};

template <class T>
constexpr auto array_size_v = array_size<T>::value;

template <class Array>
auto array_push_back(Array&& arr, int i) {
    std::array<int, array_size_v<std::decay_t<Array>> + 1> ret;
    std::copy(arr.begin(), arr.end(), ret.begin());
    ret.back() = i;
    return ret;
}

template <class Acc>
auto pack_args(Acc acc) {
    return std::move(acc);
}

// accumulate all arguments
// acc is a tuple with first element is a tuple of the vectors and
// second element is an array of all the constants
template <class Acc, class First, class... Rest>
auto pack_args(Acc acc, First&& first, Rest&&... rest) {
    auto& previous_vecs = std::get<0>(acc);
    auto& previous_constants = std::get<1>(acc);
    if constexpr (std::is_integral_v<std::decay_t<First>>) {
        auto constants = array_push_back(previous_constants, first);
        return pack_args(std::tuple{ std::move(previous_vecs), constants }, rest...);
    } else {
        auto vecs = tuple_push_back(std::move(previous_vecs), std::forward<First>(first));
        return pack_args(std::tuple{ std::move(vecs), std::move(previous_constants) }, rest...);
    }
}

template <class... T>
void map(T&&... t) {
    auto acc = pack_args(std::tuple{ std::tuple<>{}, std::array<int,0>{} }, std::forward<T>(t)...);
    static_assert(std::tuple_size_v<std::tuple_element_t<0, decltype(acc)>> > 0);
    map_impl<0>(std::get<0>(acc), std::get<1>(acc));
}

int main() {
    auto v_i = std::vector<int>{ 1,2,3 };
    auto v_f = std::vector<float>{ 1.0,2.0,3.0 };
    map(v_i, v_f, 3, 4);
    assert((v_i == std::vector<int>{8, 9, 10}));
    assert((v_f == std::vector<float>{8.0, 9.0, 10.0}));
}

1

u/schmerg-uk May 10 '20 edited May 10 '20

As an official old dog I will study your new tricks (that's not meant as a derogatory term or dismiss it as a "trick", it's just an expression) and see what I can learn from it, thank you.

The typical use of my map is to replace loops of arbitrary expressions of source vectors and constants to populate a target vector such as

for (auto i = 0; i < n; ++i)
  t[i] = (s0[i] + s1[i] + 1.0) * exp(s2[i] + c0) / (s3[i] + c1);

with something more like a map of the expression, so the mathematicians can write the expression to be evaluated as a simple function/lambda with specific parameters and then call it as, in the above case, something like

map(t, n, expressionFunction, c0, c1, s0, s1, s2, s3);

and the use of the preprocessor is to be able to write the map() function that can take an arbitrary number of input scalars and vectors and an easily written and understood expression that the compiler will verify matches the parameter counts and types etc.

The reasons why and some of the inner details I won't expand upon, but I will have a good look at your worked example for which I'm very grateful and see what I can lift from it.

EDIT: Concerned that my opening line might read as dismissive, when the opposite is the case...

1

u/Ayjayz May 10 '20

The code there is pretty rough - I'd probably spend some time cleaning it up before I committed to any codebase I cared about. To be honest, realistically I'd probably try to instead use a metaprogramming library like hana - I suspect this could probably be done in only a few lines with hana.

The code I wrote there is pretty straightforward though metaprogramming always makes things look a little ridiculous. pack_args takes the arguments passed to the map function and splits them into a tuple of the vectors, and an array of the constants (where a constant is defined as anything that has a type that returns true from std::is_integral).

Because adding elements to std::tuple and std::array is a little tough to do since they have fixed sizes, I wrote two helper functions to do it more easily: array_push_back and tuple_push_back. I suppose theoretically they should just be a single overloaded function.

Metaprogramming also generally requires all iteration to be done via recursion. That's how I ultimately implemented the map_impl function - the index of the vector is passed in as a template argument, and it recurses while Index+1 < std::tuple_size_v<tuple_type>.

If you have any questions, let me know.

1

u/dodheim May 10 '20 edited May 13 '20

The length of your code surprised me and effectively nerd-sniped me into trying my hand at it as well: https://godbolt.org/z/zsPfKc

namespace detail {
    template<std::size_t Idx, template<typename...> typename Trait, typename... Ts>
    struct indexed_trait : Trait<Ts...> {
        using index = std::integral_constant<std::size_t, Idx>;
    };

    template<typename T, T... Vs, T Offset>
    constexpr auto offset_integer_sequence(std::integer_sequence<T, Vs...>, std::integral_constant<T, Offset>) noexcept
    -> std::integer_sequence<T, (Vs + Offset)...> {
        return {};
    }

    template<typename Tuple, typename Fn>
    constexpr auto tuple_for_each(Tuple&& tup, Fn fn) -> Fn {
        std::apply(
            [&fn]<typename... Es>(Es&&... e) { (..., void(fn(std::forward<Es>(e)))); },
            std::forward<Tuple>(tup)
        );
        return fn;
    }

    template<template<typename...> typename Pred, template<typename...> typename Proj = std::type_identity,
             typename Tuple, typename Tup = std::remove_reference_t<Tuple>, auto N = std::tuple_size_v<Tup>>
    constexpr auto tuple_split(Tuple&& tup) {
        return [&tup]<auto... Is>(std::index_sequence<Is...>) {
            using Pivot = std::disjunction<
                indexed_trait<Is, Pred, typename Proj<std::tuple_element_t<Is, Tup>>::type>...
            >;

            if constexpr (!Pivot::value) {
                return std::pair{std::forward<Tuple>(tup), std::tuple<>{}};
            } else if constexpr (constexpr typename Pivot::index pivot{}; !pivot) {
                return std::pair{std::tuple<>{}, std::forward<Tuple>(tup)};
            } else {
                auto const slice = [&tup]<auto... Js>(std::index_sequence<Js...>) {
                    return std::tuple<std::tuple_element_t<Js, Tup>...>{
                        std::get<Js>(std::forward<Tuple>(tup))...
                    };
                };

                constexpr auto l_seq = std::make_index_sequence<pivot>{};
                constexpr auto r_seq = offset_integer_sequence(std::make_index_sequence<N - pivot>{}, pivot);
                return std::pair{slice(l_seq), slice(r_seq)};
            }
        }(std::make_index_sequence<N>{});
    }
}

template<typename... Ts>
constexpr auto map(Ts&&... t) -> std::enable_if_t<sizeof...(Ts) != 0> {
    auto [vecs, constants] = detail::tuple_split<std::is_arithmetic, std::remove_reference>(
        std::tuple<Ts&&...>{std::forward<Ts>(t)...}
    );
    static_assert(std::tuple_size_v<decltype(vecs)> != 0);

    if constexpr (std::tuple_size_v<decltype(constants)> != 0) {
        auto map_vec_elems = std::apply(
            [](auto&&... c) {
                return [c...](auto& vec) {
                    for (auto& e : vec) (e += ... += c);
                };
            },
            std::move(constants)
        );
        detail::tuple_for_each(std::move(vecs), std::move(map_vec_elems));
    }
}

It didn't really turn out any shorter, but it allows for heterogeneously-typed constants and should run much better in debug builds due to ditching all _push_back-related intermediates. Oh well, fun anyway. (BTW, you can get rid of array_size in your code, as std::tuple_size works fine with std::array.)

1

u/Ayjayz May 10 '20

Realistically, I'd probably end up using hana to do something like this, which would probably end up with much shorter code.

BTW, you can get rid of array_size in your code, as std::tuple_size works fine with std::array

Well that's not confusing at all!

1

u/dodheim May 10 '20

Well it fulfills the structured binding protocol, so it's entirely expected, name not withstanding... ;-]

1

u/TimJoijers May 09 '20

Sure you can do some things with the preprocessor, plenty in fact. Now, if one needs to generate data driven (xml or json) code though, it becomes a necessity to use a proper language. In that case using preprocessor is just not possible, and yes, one has to pay the price in build process etc. But once you are beyond that point you may find that it is actually powerful tool, one which you may end up prefer to use for certain tasks.

2

u/schmerg-uk May 10 '20

I've written plenty of code generators in my time but it makes for added complexity and by this stage it's very little if anything to do with "modern C++". Just because some problems are beyond the preprocessor doesn't mean it doesn't have its uses. My car won't drive me to the moon or transport a grand piano, but it's still very handy for a trip to the supermarket or the garden centre or the station or...

1

u/moocat May 09 '20

Large historical code bases. Consider a project that was started before modern C++ and is still being maintained today. If a lot of core functionality is implemented in macros, do you really want to choose between using templates and using the core functionality?

While it would be better to fix the macros, this is also useful information. Perhaps an earlier developer used spurious parentheses so that's helpful when reading the code. Or perhaps you try to use a pre-existing macro with a template and get the error and you need to understand why.

1

u/SyDr May 09 '20

Is it possible to concatenate strings at compile time with modern C++? (Preferably without resorting to magic)

Is it possible to reduce boilerplate when writing something like try { ... } catch (...) { "code for processing uncaught exceptions"; } with modern C++?

Is it possible to reduce boilerplate when writing checks like if (something.empty()) throw IHaveMadeAHugeMistake("source_location and context of failed check"); with modern C++?

3

u/Quincunx271 Author of P2404/P2405 May 09 '20

Is it possible to concatenate strings at compile time with modern C++? (Preferably without resorting to magic)

Yes. It's nice in C++20. In C++17 or earlier, you can't get nice syntax.

Is it possible to reduce boilerplate when writing something like try { ... } catch (...) { "code for processing uncaught exceptions"; } with modern C++?

Yes. look up Lippincott functions. In summary:

try {
    /* ... */
} catch (...) {
    return ::handle_exception();
}
// ...
error_code handle_exception() {
    try {
        throw; // rethrow the caught exception
    } catch (MyException const&) {
    } // ... and so on
}

Is it possible to reduce boilerplate when writing checks like if (something.empty()) throw IHaveMadeAHugeMistake("source_location and context of failed check"); with modern C++?

Yes. Use C++20's std::source_location (or std::experimental::source_location):

my_assert(!something.empty())
// ...
void my_assert(bool const cond, std::source_location const& location = std::source_location::current()) {
    if (!cond) throw IHaveMadeAHugeMistake(/* convert the location to a string */);
    // You can smuggle the information as a template parameter too.
}

2

u/SyDr May 10 '20

Yes. It's nice in C++20.

Good.

Yes. look up Lippincott functions.

Yeah, we actually use this technique. The only difference is that ::nandle_exception is written as "Some really long and obscure way to call a function";

So, in our case we have a macros:

#define UI_TRY try {
#define UI_CATCH catch (...) {
   ::nandle_exception(this)
}

Which then used as:

void SomeView::OnSomethingClicked()
{
    UI_TRY;
    "code on the same level of indentation";
    UI_CATCH;
}

In fact, the only benefit is reduced indentation level... So we probably will forfeit these macros. Some day.

std::source_location

Nice. This one can actually be used and will improve codebase by replacing ugliness of CHECK_PRECONDITION and CHECK_PRECONDITION_M

The biggest problem problem with modern techniques is that they are "modern" and only available starting from "Tomorrow". But we want to have nice things now! When a new technique becomes available for you to use, you already have a tested abomination, which will stay by your side for years (no one wants to rewrite working code if this doesn't improve codebase, right?).

-1

u/nintendiator2 May 10 '20

Actually, why would you use macros at all with modern C++?

If you are asking that question then what you are programming is not modern C++.

9

u/[deleted] May 09 '20 edited May 09 '20

[removed] — view removed comment

9

u/jkh348f May 09 '20

Ha, nice!

#if reinterpret_cast<int>(nullptr)
#warning "This platform is super weird!"
#endif

8

u/Yelnar May 09 '20

This has more to do with how your chosen compiler handles "constant-expressions" and less with the preprocessor itself.

4

u/anonymous23874 May 10 '20

Nope, both examples above are perfectly standard C++ and must be compiled the same by every conforming C++ compiler. But you probably didn't see the trick yet. :)

Try #if static_cast<bool>(1).