CppCast - Fit and Tick
Episode Date: April 2, 2015Episode number 6 of CppCast with guest Paul Fultz II recorded April 2, 2015 Paul Fultz II has developed in C++ professionally and personally in a variety of fields including DSP, web developme...nt, and desktop applications. He has developed in other languages as well such as Java, C#, Python, and Javascript but focuses most of his attention on C++ which combines correctness, expressiveness, and performance together. News Simple Extensible Pattern Matching in C++ C++Now! 2015 Program is available Simple and Clean Code vs Performance The C languages merge (April Fools!) Paul Fultz II @pfultz2 Paul Fultz II's Blog Links Tick - Trait introspection and concept creator for C++11 Fit - Header-only C++11 library that provides utilities for functions and function objects. Modern Generic Programming using the Tick and Fit libraries
Transcript
Discussion (0)
Episode number 6 of CppCast with guest Paul Foltz II, recorded April 2nd, 2015.
In this episode we talk about clean code versus performance with C++.
Then I'll interview Paul Foltz II about his upcoming C++ Now talk.
And finally, Paul and I will discuss his new C++ libraries, Tick and fit. Welcome to episode six of CppCast, the only podcast for C++ developers by C++ developers.
I'm your host, Rob Irving, and I wanted to quickly thank Anastasia Kazakova for being the guest for Episode 5 last week.
We had a great time talking about CLion, the upcoming cross-platform C++ IDE from JetBrains,
which should be hitting version 1.0 soon this spring.
So go check out Episode 5 if you haven't already. And at the start of
every episode, I like to go over some feedback. This week, I got an email from Greg. It's a bit
long. I'm not going to read the whole thing, but it starts off with, hey, really liking the CPP
podcast, man. I appreciate you taking the initiative to get it up and running.
The John Kalb interview really got things off with a bang.
Greg then goes into some constructive criticism for me.
He mentions that I have been mispronouncing a few things in past episodes,
namely Chai script, I said as Kai script.
B code, I was mispronouncing as Bicode
I apologize to all the guys over at Bcode about that
And I was also mispronouncing STL's name
And I'm really sorry about that
I believe the proper pronunciation is
Stephan Labo-Wade
So I apologize, STL
If you heard the previous episode and I mispronounced your name.
Really sorry about that.
But I do really appreciate the constructive criticism.
And if you have any feedback for me, please reach out.
You can email me at feedback at cppcast.com or reach out on Twitter at cppcast or find us on Facebook at cppcast.
So joining me today is Paul Foltz II.
Paul Foltz II has developed in C++ professionally and personally
in a variety of fields, including digital signal processing,
web development, and desktop applications.
He has developed in other languages as well,
such as Java, C Sharp, Python, and JavaScript,
but focuses most of his Java, C Sharp, Python, and JavaScript, but focuses
most of his attention on C++, which combines correctness, expressiveness, and performance
together. How are you doing tonight, Paul?
I'm doing good.
Great. So I had a couple articles I wanted to talk about tonight. The first one is simple extensible pattern matching in C++.
And this is actually just a link to someone, I think his name was Jonathan Bandella.
Yeah, John Bandella.
And he put up this library on GitHub, and it looked pretty interesting.
Paul, did you have a chance to look at this?
Yeah, yeah yeah i did um actually i found it uh
quite uh interesting the technique that he used to be able to do pattern matching using runtime
values it was it was quite fascinating yeah it was pretty interesting and he kind of called out
at the top of uh you know his document here about the pattern matching library that there already is a boost library
that can handle pattern matching, but
it kind of involves a lot of macros and it can be
a little unwieldy according to his description.
But using some modern features of C++11
and 14, he was able to get it in
I think a pretty simple header-only library, which is pretty impressive.
Well, I believe he was referring to partly the boost variant, which actually does it
more at compile time, which doesn't really work for integers because you would have to
literally create a template for every possible integer value.
So that's why this article is actually very interesting because you can actually do the
pattern matching with runtime values
and you don't have to have the whole cost of bringing in compilations
of checking every single possible value.
So that's why I found it very interesting.
Yeah, very cool.
So the next one is the C++ Now 2015 program is available.
The keynotes have been announced. Andrew
Sutton will be talking about generic
programming with concepts. Eric
Niebler with ranges for
the standard library and Tiago
Quinto on numerical weather
prediction. And there's
the full list of all the talks
and your name is in there as well, right Paul?
Yeah, yeah it is. I should be
talking on Thursday,
giving my talk on modern generic programming.
Very cool.
Yeah, it really looks like a great conference.
I hope to make it out there someday.
Yeah, just looks like it'll be a great time.
Yeah, and they have a lot of really great talks this year
about actually build system, package management.
I think some very important problems that I think need solutions in C++.
So it looks like it would be a cool conference.
Yeah, absolutely.
So the next one is simple and clean code versus performance.
And this is a long blog article just talking about performance versus performance. And this is, you know, a long blog article just talking about performance
versus efficiency, and specifically to C++. And it's kind of, you know, a debate that always goes
on, you know, whether we should be optimizing or just, you know, striving for more readable code.
Did you have a chance to read through this one, Paul? Yeah, I actually looked at that one. It was actually
fairly interesting. I guess for most application developers
generally it's better to focus on the code being
clean than trying to do a bunch of hacks and trying to get performance
out of it. Then once you kind of, in those areas
where you need to do make it faster,
I think it's important to look at like, okay, let's go to the optimizer. Let's find where the
bottleneck is first before we actually start tearing apart the code. So yeah, it's a very
good article. Yeah, exactly. And they kind of call out the 80-20 or 90-10 principle. In a common application, 90% of the time it's going to be running on just a small 10% or 20% piece of the code.
So that's the piece of code where you should use something like a profiler to find out which algorithms are taking too long and could use some efficiency.
And those are the ones that you should revisit and optimize.
But don't bother with premature optimization.
So the next one is, you know, today is April 2nd, so we're just recovering from April Fool's Day.
And as you know, you know, lots of tech companies and such like to do something on April Fool's.
Microsoft came out with the MS-DOS app for Windows
Phone. I think Google put
out a Pac-Man
game into Google Maps, which is pretty
cool. I'm not sure if Apple
did anything. I don't think they really have much of a sense
of humor. But the
ISO C++
group does have a sense of humor, and
they put out this cool article
titled, The C Languages Merge. And I read through this article, and it actually had me going
for about the first two paragraphs. Did you read this one too, Paul?
Yeah, actually, I read it too. It was a pretty funny article when you got down towards the
end about how other languages such as Rust and Go wanted to join,
but their language didn't start with C, so I think that's an important requirement when you want to create a language.
Yeah, absolutely.
Yeah, the article starts off with a somewhat believable statement saying that the C and C++ separate committees were going to join,
and that part kind of sounds believable.
And then the next article, they start off saying the Objective-C group decided they want to join in too
because they're not getting much support from Apple anymore.
And then C Sharp joins in because they were feeling left out.
So it pretty quickly becomes apparent that it is a joke, but it was pretty funny.
Yeah.
Yeah.
Okay. So Paul, you are giving
a talk at this year's C++ Now conference, as I mentioned, on modern generic programming.
What are some of the problems or pain points that you're looking to address with generics?
Yeah. Well, I think the biggest problem that most people run into when they start using templates and generic programming
is the long error messages.
Because the way it's kind of done in C++
is kind of a form of static duck typing.
So you end up with these long backtraces in the compiler,
and sometimes it leads to a lot of confusion to the users of the library,
whether they know, you know, is this an error in the library
or am I doing something wrong and what is it exactly I'm doing wrong?
So that's the first point is kind of addressing how techniques we can use now
using moderate C++ to kind of produce the clearer messages.
And the second point that I wanted to try to address is also
adding more flexibility in interfaces. Generally in the past, you know, boost libraries, they
provide a lot of flexibility, but usually under the hood, they're doing a lot of sophisticated
metaprogramming to accomplish it. So I'm looking at some things that you can do some really powerful
things with a library and you don't
have to step into deep meta programming at that point. So those are the kind of things I'm looking
to address. Okay. I've looked over some of your blog posts and I see frequent use of a couple
C++ features such as DeclType, which is one of the newer features introduced in C++11.
For listeners who aren't familiar with that feature, could you explain the use of DeclType
and why it's so important to generic programming?
The DeclType is just basically a form of typeof.
I think originally when they were proposing to C++, they wanted to actually call it type of, but it was already being used in
some compilers and they actually wanted some slightly different behavior.
So rather than cause confusion or breakage in code, they decided to give it a name that
they thought no one would use.
So they gave it the name decltype, right?
So basically you just give it an expression and then it deduces what that type is.
Why it's useful a lot of times in generic programming is basically a lot of times you're
going to have, you basically, you may not know the exact type that the user has passed
in and maybe you want to figure out what the result of the type is when they call an expression.
Like say for instance they add two numbers together.
Like does that two numbers, when you add them together, you know
does it produce the same type or
could it produce a different type, which is very
possible as well, so
like deco type actually takes care
of making sure that you can deduce those
types from
that expression and you don't have to like
go to doing meta programming
to actually figure out what those type
result types are.
That's pretty interesting.
You know, you compare it to typeof, which I'm familiar from C Sharp,
but I'm pretty certain the way C Sharp works is you do a typeof and then kind of the name of a class or something like that,
or rather the object of some class, and it would return the class.
But I don't think you could put in an expression,
and you're saying you can do that with decltype, which is pretty powerful. Yeah, yeah, yeah. You just basically put in an expression. And you're saying you can do that with decltype, which is pretty powerful.
Yeah, yeah, yeah.
You just basically put in an expression.
And basically, a decltype is basically just like a type.
So you could actually declare a variable
by calling decltype x,
and then you could put a variable y there,
and it would basically declare the variable y
with the same type as the variable x.
Interesting.
So a couple other newer c++ features that i saw you know you've using a lot in your blogs in these libraries um what
about type traits could you go over a quick explanation of that yeah type tracer is um
essentially it's it is a form of basically some type of type computation that you make about it.
The most common ones that is used in the standard library, there's the predicate ones that you can query and find out if certain aspects is true about a type, such as, you know, is the type default constructible?
Is it copy constructible?
Is it a floating point?
Things like that. But also the standard library provides other type traits,
like transformation type traits, such as like, okay,
add a const to this type or add a reference to the type,
which is usually generally useful when you're dealing with generic programming
because when you have the role type, you just stick a const in front of it.
Right?
So, yeah, that's the way type traits work.
Sure, and do those work with concept checking?
Is that kind of another C++ feature
that's related to modern generics?
Yeah, now the concept checking,
well, there's some different aspects
to the concept checking.
The concepts light proposal that they're proposing,
it hasn't been into the language yet.
And I believe they're working on finishing, I don't know what progress they have right now, but finishing what they call a technical specification, a TS.
So that will come out first, which will be kind of experimental because they want to get some kind of experience using that first.
And then eventually they'll incorporate it into the language. But I use a process of what is called substitution failure
to try to constrain the templates to try to do something very similar,
what they do with concepts like.
Although having an actual language feature,
you're going to be able to do much more powerful things
than you can do as a library.
But, yeah.
Okay, so let's talk a little bit more about the two libraries you're working on.
The first one being TIC.
What are the goals of the TIC library?
So the TIC library is basically a way to build type traits,
concept-based type traits in C++.
So it's a way to specify those,
and then secondly provide a mechanism to check them.
And one of the ways it's built to specify
is trying to use a very
simple syntax. It's actually very similar to
the way that Concepts Lite work. It's actually based
off, originally off of
Eric Knebler's blog he had about
doing concept checking. So it's kind of
based on that. It is slightly different.
It has some slightly difference. It's kind of evolved
a little differently than his library,
but it works very similarly.
And then at the same time, you can actually,
when you have a function or a class and you want to constrain it
based upon those types, you can actually do that as well.
It has a mechanism to do that.
Okay.
Are there any existing libraries that provide similar functionality to TIC?
And if so, what advantages do you see TIC having?
Some of the other libraries, one of the very classic libraries that's used is Boost Concept Check.
It's a library that's used now.
It's fairly outdated because sometimes you can get, it doesn't really actually stop error messages. It just basically inserts kind of a thing into the error that says,
hey, you failed the usage requirements for it to try to give you a better clue
that, okay, this is why I can't call this function.
And there's some other limitations as well,
such as it doesn't support overloading and stuff like that.
But it's written like
almost a decade ago, so it's still
it's
somewhat outdated now that we've got
new techniques in C++11
and C++14, so this library
actually tries to take advantage of that.
The other example
of a library that's similar is what's in Eric
Knebler's Ranges V3 library
and it's kind of based off of his Knebler's ranges V3 library. And that,
and it's kind of based off of his idea.
So it's actually,
uh, fairly similar.
So.
Okay.
Uh,
so apparently one library is not enough for you to keep you busy.
You're also working on fit,
which you've labeled as a C plus plus function library.
Can you tell us about that one?
Yeah,
the main,
uh,
the main focus of that is actually Yeah, the main, uh, the main focus of that
is actually provides several utilities for, uh, uh, functions in C plus plus a lot of the
inspiration for the library came from an older library that was originally proposed to boost
called a boost egg. And it actually provided all these really useful utilities. And so I kind of,
kind of basically build a more modern version of that
and actually started focusing on a lot more of the problems
that you actually face in C++.
I actually do call it a function utility library
rather than just a functional library
because it focuses more on solving problems
that you actually face naturally in C++,
such as initialization of lambdas, overloading, extension-like methods,
kind of things that C++ programmers would do.
It does have some functional utilities,
but most libraries that traditionally have done functional programming,
like say BoostPhoenix, usually require subscribing to a whole entire framework
to be able to do that or it
provides other things like all the functions are automatically curable and things like that which
the fit library doesn't do that you can do that but you you kind of have to explicitly say you
want that kind of feature there so it's kind of built in a more lightweight way so that you can actually focus on solving problems actually in
C++. Okay. So what are the things that you can do when using the Fit and Tick libraries? I saw
you had one post on your blog. I think it was titled, Look Ma, No Metaprogramming?
Yeah. So that actually had some very interesting things. I found some very simple ways that you could solve some problems that I've solved previously before that usually involved amounts of metaprogramming.
One example is a generic way of doing a find iterator function so that, like, for example, you would want, you know, in general, yeah,
you could call us to define, but it's linear search, right. But on like maps or sets or
whatever, hash tables, right. You want to be able to find using, they have a, their own find method
to be able to search more efficiently. So it shows kind of a mechanism, like how you could
actually build that just using basic function overloading and actually actually doesn't even use the tick
library because the type requirements you kind of just embed right into the the decal type and i
kind of extend it even further and i show example of actually creating like a recursive print
function that can like go over tuples and ranges and and print out values uh and whatever is is
possible so and all that is just done by simple function overloading and deco type,
and it doesn't actually, you don't need to go to,
like previously when you tried to solve those problems,
usually it involved some amount of metaprogramming
to try to compute all this type and logic.
And so now you can do it directly just doing basic function overloading.
Very cool.
So are you hoping to get both of these libraries added to Boost?
How does that process work exactly?
Yeah, actually, I would eventually try to get it into Boost.
The process I'm not quite as familiar with.
My assumption is you just kind of write an email like, hey, I've got this library maybe people are interested in.
And then eventually maybe people like, okay, we're interested in this library.
And then you put out a request that you want to formal review.
And then you get like a review manager.
And then they'll set up a peer review of the library.
And you'll get feedback and have to go back.
And if they,
if they do approve it,
generally they'll have changes you'll need to make for it to actually get into
the library and into boost.
So,
um,
I think that's how the process works from my understanding.
Have you heard of,
uh,
the project Robert Ramey started the boost library incubator?
Yeah, actually I have. Both of those libraries are actually
on that website. He actually contacted me
because he saw me posting about it on the Boost mailing list.
He contacted me about it and so I actually put it up on there as well.
Very cool. One thing I saw
on GitHub is you mentioned that you were testing these libraries using GCC and Clang. Does the Visual Studio VC compiler lack some of the libraries is what is called expressions finet
and from what i've heard from visual studio developers um they're probably that's probably
like one of the last things that they're going to probably finish so it will probably be a while
before they do that now expressions finet is like really integral to the TIG library. The FIT library does rely on Expressions Finite,
but most of it involves just checking the callability
of a function object.
And I know Eric Knebler has a trick
where you can use some kind of implicit conversion
to a function pointer,
and you can actually do it without using Sphinae.
So there is a possibility you could get the Fit Library
to support it.
That work around that part.
And the other aspect is
Cons Expert, which is
if there's not good support in Visual Studio
it could possibly be disabled
the Cons Expert support in the Fit Library.
Right now, I just
kind of use this for my own.
In the free time, and I don't have a need for using Visual Studio, but if somebody actually wanted to fund or send a pull request for adding the Visual Studio support, it's very possible that that could happen.
Very cool. So, Paul, what keeps you excited about C++? I think a lot of the things that I like doing, too, is focus on doing algorithms, solving computational problems.
There's a lot of interesting problems involved in that area.
Also, some of the other areas of stuff that I would like to be actually working on in the future with C++ is possibly building a library that can kind of encompass database queries and ranges
and other kinds of things together, kind of similar to Lync or something like that,
but in kind of a more C++ kind of style.
It's something that I've thought about for a while,
which I think would be something very, very useful in C++ as well.
I'm familiar with link in C Sharp.
That would definitely be useful to have something like that in C++.
Very cool.
Because I did implement a link version of the library back a couple years ago.
It just mainly works over ranges.
It uses boost range, basically.
And I've looked at trying to like kind of extend it like how C sharp does to
be able to support,
you know,
database queries and all kinds of stuff.
Right.
But the current design of it,
I don't think really gels well with C sharp.
So sometimes I spent time thinking about how you could do it in a much more
C plus plus way that kind of
blends in better with C++. So yeah. Very cool. Is there anything else you want to talk about today?
I was taking a look at your blog and you seem to be very active there. It seems like you're
putting out almost one post every week and they're all very informative, deep posts.
So really good job on your blog.
Okay, yeah, thanks.
I haven't put out a lot recently because I've been actually working on getting my stuff ready for C++ now.
Sure, sure.
But I'm hoping to get back to it here in the next month or so.
So, yeah.
Cool.
Well, where can people find you online if they want to learn more about you
uh the most of the stuff you can go to my website at pfoltz2.com slash blog that's basically has
my blog and there's a there's a there's a link to my twitter link to my github um right there
and i think a link to my email that you can email me and contact me as well.
Okay, great. Thanks for coming on the show, Paul.
Yeah, sure. No problem.
Thanks so much for listening as we chat about C++.
I'd love to hear what you think of the podcast.
Please let me know if we're discussing the stuff you're interested in, or if you have a suggestion for a topic, I'd love to hear that also.
You can email all your thoughts to feedback at cppcast.com.
I'd also appreciate if you can follow CppCast on Twitter and like CppCast on Facebook.
And of course, you can find all that info and the show notes on the podcast website at cppcast.com.
Theme music for this episode is provided by podcastthemes.com.