March 2015

Quick Q: Calling C++ Application Code from C library?

Quick A: You need to create a stub.

Recently on SO:

Calling C++ Application Code from C library?

Folks, Assuming I have a c++ application / library running which implements say

/* Alloc API */
void* my_alloc(int size) {
    return malloc(sizeof(size));
}

This is not under "extern c".

I have a C dynamic library from which I need to call my_alloc, can I directly call that API?

Like,

int test_my_alloc (int size) {
    int *x;

    x = (int*)my_alloc(size);
    if (x == NULL) {
        return 0;
    } else {
        return 1;
    }
}

CppCon 2014 Hardening Your Code--Marshall Clow

While we wait for CppCon 2015 in September, we’re featuring videos of some of the 100+ talks from CppCon 2014. Here is today’s feature:

Hardening Your Code

by Marshall Clow

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

Ok, you've written some code, and it seems to work. How can you be sure that it works? It's a busy, complicated, dangerous world out there, and software has to work in lots of different environments.

How can you gain confidence about your code? How can you make your code more reliable?

There are a lot of techniques available to developers today; I'll talk about several of them: Unit tests, static analysis, runtime analysis, fuzzing, decoding compiler warnings and probably others.

CppCon 2014 Creative Coding with C++--Andrew Bell

While we wait for CppCon 2015 in September, we’re featuring videos of some of the 100+ talks from CppCon 2014. Here is today’s feature:

Creative Coding with C++

by Andrew Bell

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

Realtime graphics, computer vision, hardware hacking, and audio synthesis are just a few of the crafts that fall under the banner term of "creative coding". In this session we'll talk about some of the creative projects putting C++ in places you might not expect it - everywhere from the Smithsonian's permanent design collection to robotic Coca-Cola dispensers on California beaches. We'll look at a wide spectrum of projects ranging from those of the "maker" community to commercial work from full-time professionals earning their livings in advertising and design agencies. And finally we'll take a look at how you can use the C++ you already know to jumpstart your own creative coding projects using the open source toolkit Cinder.

Stackless coroutines with Visual Studio 2015 -- Paolo Severini

From a totally unnecessary blog (we beg to differ):

Stackless coroutines with Visual Studio 2015

by Paolo Severini

From the article:

I had been looking for some time now at the problem of implementing coroutines/resumable functions in order to have even in C++ something similar to what is provided by C# await and yield statements. It turns out that -- unbeknown to me -- this is quite a hot topic in the C++ community...

... stackful coroutines have a big disadvantage in the fact that fibers are very expensive... The new proposal (N4286) instead focuses mostly on a stackless implementation, and promises to be scalable to billions of concurrent coroutines...

A quick tour of the Silicon web framework: A simple blog API in 85 C++ lines -- Matthieu Garrigues

siliconwebfx.PNGBecause clear example code is a great motivator:

A quick tour of the Silicon web framework: A simple blog API in 85 C++ lines

by Matthieu Garrigues

From the article:

In late January 2015, I released the first version of the Silicon Web Framework. The documentation covers all the concepts of the library but does not contains a concrete example covering the needs of a real world application. In this blog post, I'll show how to write a such an application with the framework. Like most modern web apps, it relies on a database to store data, and sessions to authenticate its users.

The source code of this article is hosted on the Silicon github repository: https://github.com/matt-42/silicon/blob/master/examples/blog_api.hh ...

Secrets of the Conditional (ternary) Operator -- Alex Kulikov

Hot off kukuruku:

Secrets of the Conditional (ternary) Operator

by Alex Kulikov

(Note: Translation of the original Russian-language article here.)

From the article:

Every self-respecting C/C++ programmer knows what the ternary operator is, and most everyone used it at least once in their programs. But do you know all the secrets of the ternary operator? What potential dangers are associated with its use and what features, seemingly not related to its direct purpose, it has? This article gives you the opportunity to test your knowledge and maybe learn something new.

Let's start with a small test...

Quick Q: Why can a constexpr function return a non-constant?

Quick A: Because constexpr means can be evaluated at compile time, not that it will be. And it won’t be if the inputs aren’t compile-time constants.

Recently on SO:

I am confused about a constexpr function?

In C++ Primer, Fifth Edition, §6.5.2:

A constexpr function is defined like any other function but must meet certain restrictions: The return type and the type of each parameter in must be a literal type (§2.4.4, p. 66), and the function body must contain exactly one return statement

but another sentence in this chapter (page 239):

A constexpr function is permitted to return a value that is not a constant

// scale(arg) is a constant expression if arg is a constant expression
constexpr size_t scale(size_t cnt) { return new_sz() * cnt; }

Is it a contradictory summary? I am confused about it.
The return type of scale is literal type?
update: what's the difference between literal type and constant ?

CppCast Episode 2: Jason Turner on ChaiScript and Cross Platform C++ -- Bob Irving

Episode 2 of CppCast, the only podcast by C++ developers for C++ developers. In this episode host Rob Irving interviews Jason Turner about ChaiScript and the benefits of taking your C++ application cross platform.

CppCast Episode 2: Jason Turner on ChaiScript and Cross Platform C++

by Rob Irving

About the interviewee:

Jason has been developing portable C++ since 2002. With very few exceptions, every line of code he has written since then has had to run on multiple platforms. He is an independent contractor focusing on cross-platform issues, utilization of C++ libraries from scripting languages and code quality assurance. He is the co-creator and maintainer of ChaiScript, a mature scripting language designed for modern C++. His latest project is cppbestpractices.com: a fledgling effort to gather the collective wisdom of the C++ community.

CppCon 2014 The Philosophy of Google's C++ Code--Titus Winters

While we wait for CppCon 2015 in September, we’re featuring videos of some of the 100+ talks from CppCon 2014. Here is today’s feature:

The Philosophy of Google's C++ Code

by Titus Winters

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

The Google C++ Style Guide is a fairly popular guide for C++ coding practices, both at Google and externally, but some of its recommendations often seem dated and have created controversy and perceived tension with more modern C++ In this talk we will focus on the core philosophies underlying that guide, ranging from the common (be consistent) to the unusual (leave an explicit trace for the reader), and debunk the idea that Google's C++ is anything less than modern. We'll discuss how these core ideas inform contentious rules like "No non-const references" and "Don't use exceptions," and how the application of those rules has worked for us in practice, both as developers and reliability engineers (SREs).