September 2018

CopperSpice: Constexpr Static Const

New video on the CopperSpice YouTube Channel:

Constexpr Static Const

by Barbara Geller and Ansel Sermersheim

About the video:

This presentation covers constexpr and how it differs from const. We also discuss what constexpr means in practice versus what is actually required by the standard, a subject which is often misunderstood. The topic of constexpr vs preprocessor macros is also covered with suggestions for best practices.

Please take a look and remember to subscribe!

Microsoft Cognitive Services C++ SDK--Sebastiano Galazzo

Are you wondering how to use the Microsoft Cognitive Services with C++?

Microsoft Cognitive Services C++ SDK

by Sebastiano Galazzo

Introduction:

The project is a wrapper to use Microsoft Cognitive Services in standard C++ as is currently not supported.

The project provides full support to Computer Vision API. A wrapper to gphoto2 will provide the full control digital cameras (DSLR), getting the raw shot and making manipulations with Computer Vision API.

Available camera-ai, a working commandline example to take a picture from the camera conneced by USB and analyze by Cognitive Services.

A lot of people have a better C++ than mine, who wants to contribute is welcomed!

What’s New in ReSharper C++ 2018.2

While ReSharper C++ 2018.1 introduced two major new features, debug step filters and includes analyzer, ReSharper C++ 2018.2 is focused on improving its understanding of the C++ language. C++17 and C++20 got some special attention. However, the biggest highlight is the long-awaited support for C++/CLI.


What’s New in ReSharper C++ 2018.2

by Igor Akhmetov

From the article:

This release introduced:

  • Initial C++/CLI support
  • C++17 features: class template argument deduction, fold expressions, auto non-type template parameters, pack expansions in using declarations, Using declarations with multiple declarators, guaranteed copy elision, and more
  • C++20 features: coroutines, designated initialization, and some others
  • Integrated spell checking with ReSpeller
  • Formatting inspections

 

 

Quick Q: C++11 auto: what if it gets a constant reference?

Quick A: auto never deduces a reference.

Recently on SO:

C++11 auto: what if it gets a constant reference?

Read this article: Appearing and Disappearing consts in C++

Type deduction for auto variables in C++0x is essentially the same as for template parameters. (As far as I know, the only difference between the two is that the type of auto variables may be deduced from initializer lists, while the types of template parameters may not be.) Each of the following declarations therefore declare variables of type int (never const int):

auto a1 = i;
auto a2 = ci;
auto a3 = *pci;
auto a4 = pcs->i;

During type deduction for template parameters and auto variables, only top-level consts are removed. Given a function template taking a pointer or reference parameter, the constness of whatever is pointed or referred to is retained:

template<typename T>
void f(T& p);

int i;
const int ci = 0;
const int *pci = &i;

f(i);               // as before, calls f<int>, i.e., T is int
f(ci);              // now calls f<const int>, i.e., T is const int
f(*pci);            // also calls f<const int>, i.e., T is const int

This behavior is old news, applying as it does to both C++98 and C++03. The corresponding behavior for auto variables is, of course, new to C++0x:

auto& a1 = i;       // a1 is of type int&
auto& a2 = ci;      // a2 is of type const int&
auto& a3 = *pci;    // a3 is also of type const int&
auto& a4 = pcs->i;  // a4 is of type const int&, too

Since you can retain the cv-qualifier if the type is a reference or pointer, you can do:

auto& my_foo2 = GetFoo();

Instead of having to specify it as const (same goes for volatile).

Edit: As for why auto deduces the return type of GetFoo() as a value instead of a reference (which was your main question, sorry), consider this:

const Foo my_foo = GetFoo();

The above will create a copy, since my_foo is a value. If auto were to return an lvalue reference, the above wouldn't be possible.

Using C++17 Parallel Algorithms for Better Performance--Billy O’Neal

Are you using the parallel capacities of the std?

Using C++17 Parallel Algorithms for Better Performance

by Billy O’Neal

From the article:

C++17 added support for parallel algorithms to the standard library, to help programs take advantage of parallel execution for improved performance. MSVC first added experimental support for some algorithms in 15.5, and the experimental tag was removed in 15.7.

The interface described in the standard for the parallel algorithms doesn’t say exactly how a given workload is to be parallelized. In particular, the interface is intended to express parallelism in a general form that works for heterogeneous machines, allowing SIMD parallelism like that exposed by SSE, AVX, or NEON, vector “lanes” like that exposed in GPU programming models, and traditional threaded parallelism.

Our parallel algorithms implementation currently relies entirely on library support, not on special support from the compiler. This means our implementation will work with any tool currently consuming our standard library, not just MSVC’s compiler. In particular, we test that it works with Clang/LLVM and the version of EDG that powers Intellisense...

How To Use std::visit With Multiple Variants--Bartlomiej Filipek

Variant utility.

How To Use std::visit With Multiple Variants

by Bartlomiej Filipek

From the article:

std::visit is a powerful utility that allows you to call a function over a currently active type in std::variant. It does some magic to select the proper overload, and what’s more, it can support many variants at once.

Let’s have a look at a few examples of how to use this functionality...

CppCon 2017: Fantastic Algorithms and Where To Find Them--Nicholas Ormrod

Have you registered for CppCon 2018 in September? Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2017 for you to enjoy. Here is today’s feature:

Fantastic Algorithms and Where To Find Them

by Nicholas Ormrod

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

Come dive into some exciting algorithms — tools rare enough to be novel, but useful enough to be found in practice. Want to learn about "heavy hitters" to prevent DOS attacks? Come to this talk. Want to avoid smashing your stack during tree destruction? Come to this talk. Want to hear war stories about how a new algorithm saved the day? Come to this talk! We'll dive into the finest of algorithms and see them in use — Fantastic Algorithms, and Where To Find Them.

Quick Q: C++ constexpr - Value can be evaluated at compile time?

Quick A: if parameter are not known at compile time, it is like a normal function

Recently on SO:

C++ constexpr - Value can be evaluated at compile time?

The quoted wording is a little misleading in a sense. If you just take PlusOne in isolation, and observe its logic, and assume that the inputs are known at compile-time, then the calculations therein can also be performed at compile-time. Slapping the constexpr keyword on it ensures that we maintain this lovely state and everything's fine.

But if the input isn't known at compile-time then it's still just a normal function and will be called at runtime.

So the constexpr is a property of the function ("possible to evaluate at compile time" for some input, not for all input) not of your function/input combination in this specific case (so not for this particular input either).

It's a bit like how a function could take a const int& but that doesn't mean the original object had to be const. Here, similarly, constexpr adds constraints onto the function, without adding constraints onto the function's input.

Admittedly it's all a giant, confusing, nebulous mess (C++! Yay!). Just remember, your code describes the meaning of a program! It's not a direct recipe for machine instructions at different phases of compilation.

(To really enforce this you'd have the integer be a template argument.)

C++ Links #1--Bartlomiej Filipek

Things to look at.

C++ Links #1

by Bartlomiej Filipek

From the article:

I'd like to make an experiment on the blog and introduce a new simple series. Each Friday you'll see a summary with valuable links and resources from the C++ World. The links and annotations are coming from a guest author - Wojciech Razik - one of the co-author of cpp-polska.pl.