CppCon 2018: Richard Powell “Named Arguments from Scratch”

preview_player
Показать описание


Given a function: void foo(int a, int b);

Wouldn't it be great to be able to write? foo("b" = 3, "a" = 2);

This will be a live coding session where we start from the basic idea of what we want to do and build up a small library similar that accomplishes this goal. We will outline some simply but powerful programming techniques and give a brief outline on how to use Hana, a powerful library for metaprogramming.

Richard Powell, Audio Software Engineer

I started using C++ 10 years ago to write a psychoacoustic audio encoder/decoder and have continued to explore how to make software that unlocks the potential of hardware to bring amazing applications to life. I graduated from UC Berkeley with a BS in Electrical Engineering and Computer Science, and worked throughout the Bay Area for the past 15 years. I enjoy teaching and learning about C++ and programming.

*-----*
Рекомендации по теме
Комментарии
Автор

Umm... that’s not how Objective-C works... (or Swift, for that matter - same runtime). “Named arguments” are a feature provided by the compiler’s parser stage; what’s actually going on is the argument name is part of the method name. So, for example, in the call [foo doSomethingWithThisInt:5 anotherInt:7]; to a method declared anotherInt:(int)b; the entire method name (it’s selector, in Objective-C terminology) is and it’s C function signature is void f(id self, int a, int b). The arguments are passed according to standard platform C ABI rules. The only thing objc_msgSend() does is find the C function that corresponds to the selector value passed to it, and jump to that function.

P.s. - I *think* the selector is passed to the C function as well, but it’s been a while since I read the relevant sections of the Runtime Reference... 🤔

darkwinter
Автор

i wish i could just include and link any c++ library by a simple click like it is done here.

videojeroki
Автор

I tried to reproduce code samples without boost::hana, but after adding apply(auto &f) compiler started generating foo and stopped optimizing it into returning 42 (on clang(trunk) and gcc(8.2); gcc(trunk) and some experimental clangs did succeed).


GrzesiuG
Автор

Can you determine the name of Arguments in C++ (without meta classes)?

petermuller
Автор

Works well in Matlab: makes the intent of each argument clear without having to check the API.

williamchamberlain