How to pass a template function in a template argument list

I suspect the answer is “you cannot do this”. Yes, that is the case, you cannot pass a function template as a template argument. From 14.3.3: A template-argument for a template template-parameter shall be the name of a class template or an alias template, expressed as id-expression. The template function needs to be instantiated before … Read more

When is a compiler-only memory barrier (such as std::atomic_signal_fence) useful?

To answer all 5 questions: 1) A compiler fence (by itself, without a CPU fence) is only useful in two situations: To enforce memory order constraints between a single thread and asynchronous interrupt handler bound to that same thread (such as a signal handler). To enforce memory order constraints between multiple threads when it is … Read more

Meaning of default initialization changed in C++11?

The final effects are almost the same. In C++03, the use of default-initialize was restricted to non-POD class type, so the last point never applied. In C++11, the standard simplifies the wording by eliminating the condition with regards to where default-initialization was used, and changes the definition of default-initialization to cover all of the cases … Read more

Dynamic allocation of class array with protected destructor

Rejecting an array-new with a protected destructor is correct, as per C++11, §5.3.4 ¶17: If the new-expression creates an object or an array of objects of class type, access and ambiguity control are done for the allocation function, the deallocation function (12.5), and the constructor (12.1). If the new expression creates an array of objects … Read more

Best alternative to std::optional to return an optional value from a method? (using C++98/C++11/C++14)

std::optional, like its boost::optional parent, is a pretty basic class template. It’s a bool, some storage, and a bunch of convenience member functions most of which are one line of code and an assert. The DIY option is definitely preferred. (1) involves allocation and (2), (3) involve having to construct a T even if you … Read more

Tools to generate higher-quality error messages for template-based code?

Let’s have a stab at an answer (I marked this community wiki so we get a good response together)… I’m working since a long time with templates and error messages have generally improved in some way or another: Writing a stack of errors creates a lot more text but also typically includes the level the … Read more

What if the lambda expression of C++11 supports default arguments?

There is no real reason that lambdas can’t have default arguments. However, there are two main ways to use lambdas, and only one of them would allow default arguments without changing the type system. You can call the lambda directly, or through a template. Default parameters would work fine in this case. You can call … Read more

floating-point promotion : stroustrup vs compiler – who is right?

I don’t know what exactly Stroustrup’s book says, but according to the standard, floats will not be converted to doubles in this case. Before applying most arithmetic binary operators, the usual arithmetic conversions described in 5p9 are applied: If either operand is of scoped enumeration type (7.2), no conversions are performed; if the other operand … Read more

C++ Forward declare using directive

You can’t declare a using alias without defining it. You can declare your class template without defining it, however, and use a duplicate using alias: namespace fancy { template <typename> class Foo; class Bar; using FooBar = Foo<Bar>; }

Is there any difference between structure and union if we have only one member?

In C: None. The famous “space-saving joke” #define struct union is almost not a joke. In C++98: Unions can only have POD members, non-union classes can have arbitrary members. In C++11: Unions can have arbitrary data members of object type (but not of reference type), but their use is more restricted that that of non-union … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)