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).

Quick Q: How to write variadic template constructor?

Quick A: use an std::initializer_list.

Recently on SO:

Writting variadic template constructor

Recently I asked this question but now I would like to expand it. I wrote the following class:

template <class T>
class X{
public:
    vector<T> v;
    template <class T>
    X(T n) {
        v.push_back(n);
    }
    template <class T, class... T2>
    X(T n, T2... rest) {
        v.push_back(n);
        X(rest...);
    }
};

When creating and object using

X<int> obj(1, 2, 3);  // obj.v containts only 1

Vector only contains the first value, but not others. I've checked and saw that constructor is called 3 times, so I'm probably creating temp objects and filling their vectors with the rest of the arguments. How do I solve this problem?

Quick Q:How to modify a tuple in a vector of tuples c++?

Quick A: Capture the tuple by reference.

Recently on SO:

Modifying a tuple in a vector of tuples c++

I have a vector of tuples vector<tuple<int,int>> vector; and I want to modify one of the tuples it contains.

for (std::tuple<int, int> tup : std::vector)
{
    if (get<0>(tup) == k)
    {
        /* change get<1>(tup) to a new value
         * and have that change shown in the vector
         */
    }
}

I am unsure how to change the value of the tuple and have the change be reflected in the vector. I have tried using

get<1>(tup) = v;

but that doesn't change the value of the tuple that is in the vector. How can I do this? Thanks.

How to write a standard-like algorithm -- Indi

Explicit C++ has posted a nice tutorial on how to implement an algorithm in C++.

How to write a standard-like algorithm

by Indi

from the article:

Writing a standard-like algorithm should be one of the key parts of a modern C++ beginner’s course outline. This post will be a whirlwind guide through the steps toward creating a standard-like algorithm. The focus is not on the algorithm itself, but on the process of creating one.

CppCon 2014 ...Scaling Visualization in concurrent C++ programs--Fedor G Pikus

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:

...Scaling Visualization in concurrent C++ programs

by Fedor G Pikus

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

High performance is one of the main reasons programmers choose C++ for their applications. If you are writing in C++, odds are you need every bit of computing power your hardware can provide. Today, this means writing multi-threaded programs to effectively utilize the multiple CPU cores that the hardware manufacturers keep adding. Everyone knows that writing multi-threaded programs is hard. Writing correct multi-threaded programs is even harder. Only after spending countless hours debugging race conditions and weird intermittent bugs do many programmers learn that writing efficient multi-threaded programs is harder yet. Have you ever wanted to see what are all your threads doing when they should be busy computing? This talk will show you how.

We begin by studying several techniques necessary for collecting large amounts of data from the running program very efficiently, with little overhead and minimal disruption to the program itself. We will cover efficient thread-safe memory management and efficient thread-safe disk I/O. Along the way we dabble in lock-free programming just enough to meet our needs, lest the subject will spiral into an hour-long talk of its own. With all these techniques put together, we can collect information about what each thread is doing, which threads are computing and what exactly, and which threads are slacking off waiting on locks, and do it at the time scale of tens of microseconds if necessary. Then we process the collected data and create a timeline that shows exactly what the program was doing at every moment in time.