c++17

Defining global constants in C++1z?

假如想象 提交于 2019-12-05 02:49:27
What is the best way of declaring memory efficient Global Constants in C++1z which do not make internal linkage , so a single copy is used though out all the translation units ? Although it's been mentioned in many places, we didn't have any singular "The best approach" question and answer, so here it is. Here is a partial list of places where I have found related questions. constexpr global constants in a header file and odr global declarations/initializations using static, const, constexpr Global Constants in C++11 Defining global constant in C++ c++ global constants issue Global Constants

Passing lambda as template parameter to templated by function-pointer function

早过忘川 提交于 2019-12-05 02:42:27
Looks like I cannot pass a no-capture lambda as a template parameter to a templated by function-pointer function. Am I doing it the wrong way, or is it impossible? #include <iostream> // Function templated by function pointer template< void(*F)(int) > void fun( int i ) { F(i); } void f1( int i ) { std::cout << i << std::endl; } int main() { void(*f2)( int ) = []( int i ) { std::cout << i << std::endl; }; fun<f1>( 42 ); // THIS WORKS f2( 42 ); // THIS WORKS fun<f2>( 42 ); // THIS DOES NOT WORK (COMPILE-TIME ERROR) !!! return 0; } It's mostly a problem in the language's definition, the following

In the expression left() = right(), why is right() sequenced first?

眉间皱痕 提交于 2019-12-05 01:42:25
In C++, the expression left() = right() evaluates right() left() in that sequence. The right() goes first, as has been discussed here. I cannot think of a reason for right() to go first. Can you? I presume that there exists a reason. Otherwise, the standard would hardly say what it says, but consider: right() will return some result. At the machine-code level, does the CPU not need to know where to put the result right() will return before asking right() to return it? If you happen to know what the standard committee was thinking (because you were in the room or have read the memo), that's

Applying multiple tuples to the same function (i.e. `apply(f, tuples…)`) without recursion or `tuple_cat`

我与影子孤独终老i 提交于 2019-12-05 01:35:11
std::experimental::apply has the following signature: template <class F, class Tuple> constexpr decltype(auto) apply(F&& f, Tuple&& t); It basically invokes f by expanding t 's elements as the arguments. I would like something that does the exact same thing, but with multiple tuples at the same time: template <class F, class... Tuples> constexpr decltype(auto) multi_apply(F&& f, Tuples&&... ts); Example usage: std::tuple t0{1, 2, 3}; std::tuple t1{4, 5, 6}; auto sum = [](auto... xs){ return (0 + ... + xs); }; assert(multi_apply(sum, t0, t1) == 1 + 2 + 3 + 4 + 5 + 6); I can think of various

False-branch of if constexpr not discarded in templated lambda

女生的网名这么多〃 提交于 2019-12-05 01:18:15
I have a problem with "if constexpr" in a templated lambda. For the sake of argument let's ignore how I got there, but I have a struct foo that is defined in some way to result in something as follows: template<bool condition> struct foo { int a; // Only contains b if condition is true int b; } Now I can define a templated function thtemplate template<bool condition> void print_fun(foo & obj) { /* Do something with obj.a */ if constexpr(condition) /* Do something with obj.b */ }; Instantiating this function and using it will compile, if the constexpr parameter to foo is the same as the one to

Class template argument deduction failed with derived class

旧街凉风 提交于 2019-12-05 01:03:59
#include <utility> template<class T1, class T2> struct mypair : std::pair<T1, T2> { using std::pair<T1, T2>::pair; }; int main() { (void)std::pair(2, 3); // It works (void)mypair(2, 3); // It doesn't work } Is the above well formed? Is it possible deduce the class template arguments in the second case if the constructors are being inherited? Are the constructors of std::pair participating in the creation of implicit deduction guides for mypair ? My compiler is g++ 7.2.0. I think this is a gcc bug (or at least a minor core language wording defect). Inherited constructors do count as

Why Sortable concept requires totally ordered value type, while std::sort only requires “less than” comparable?

99封情书 提交于 2019-12-04 23:55:19
In the latest paper on concepts N3701 , there is the following example with the sort algorithm: template<typename Cont> requires Sortable<Cont>() void sort(Cont& cont) where Sortable concept is defined as template<typename T> concept bool Sortable() { return Permutable_container<T>() && Totally_ordered<Value_type<T>>(); } where Totally_ordered , not surprisingly, is defined as template<typename T> constexpr bool Totally_ordered() { return Weakly_ordered<T>() && Equality_comparable<T>(); } and in turn Equality_comparable is defined as template<typename T> constexpr bool Equality_comparable() {

Short circuit of overloaded operator && and || in C++17

妖精的绣舞 提交于 2019-12-04 23:54:50
I read in http://en.cppreference.com/w/cpp/language/operators : The boolean logic operators, operator && and operator || Unlike the built-in versions, the overloads do not sequence their left operand before the right one, and (until C++17) cannot implement short-circuit evaluation. (My emphasis). Couldn't find any resource or code example for C++17 supporting short-circuit for operator&& and operator||. Is it related to C++17 parameter pack fold expression? tried to play with it but couldn't create short circuit behavior for overloaded operator && and || with C++17 fold expression. Code: class

How you convert a std::string_view to a const char*?

痞子三分冷 提交于 2019-12-04 23:51:40
Compiling with gcc-7.1 with the flag -std=c++17 , the following program raises an error: #include <string_view> void foo(const char* cstr) {} void bar(std::string_view str){ foo(str); } The error message is In function 'void bar(std::string_view)': error: cannot convert 'std::string_view {aka std::basic_string_view<char>}' to 'const char*' for argument '1' to 'void foo(const char*)' foo(str); I'm surprised there is no conversion to const char* because other libraries (abseil, bde), provide similar string_view classes which implicitly convert to const char* . A std::string_view doesn't provide

incomplete types with std::map and std::variant

倾然丶 夕夏残阳落幕 提交于 2019-12-04 23:37:55
Consider this simplified and very specific implementation of a recursive variant on top of std::variant : #include <map> #include <variant> struct recursive_tag; template <typename...> struct RecursiveVariant; template <> struct RecursiveVariant<int, std::map<int, recursive_tag>> : std::variant<int, std::map<int, RecursiveVariant<int, std::map<int, recursive_tag>>>> { using underlying = std::variant<int, std::map<int, RecursiveVariant<int, std::map<int, recursive_tag>>>>; using underlying::underlying; }; int main() { RecursiveVariant<int, std::map<int, recursive_tag>> rv; } This fails to