c++17

C++ Library & Self registering classes: Factory map empty in client application

最后都变了- 提交于 2020-01-30 10:48:27
问题 Let there be a C++ library (let's call it lib ) which gets included as a static library in an application (let's call it app ). Within the lib there's a base class node . Each subclass of a node is identified by a UUID. I employ a self registering pattern to ensure that new classes register themselves at the factory. The factory allows to build a node subclass object based on the provided UUID. The app builds objects through the lib 's factory::build() function. My factory is based on the

'for_each_n' is not a member of 'std' in C++17

柔情痞子 提交于 2020-01-29 17:40:43
问题 I have small piece of code for std::for_each_n loop. I tried running it on inbuilt Coliru compiler GCC C++17 using following command : g++ -std=c++1z -O2 -Wall -pedantic -pthread main.cpp && ./a.out But compiler give an error that " 'for_each_n' is not a member of 'std' ". My code is bellow which is copied from cppreference. #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> ns{1, 2, 3, 4, 5}; for (auto n: ns) std::cout << n << ", "; std::cout << '\n';

When should I use std::any

旧巷老猫 提交于 2020-01-29 06:34:06
问题 Since C++17 std::any is introduced. One can now write code like this #include <iostream> #include <any> #include <string> int main () { const double d = 1.2; std::any var = d; const std::string str = "Hello World"; var = str; } A double is assigned to the variable var and than a std::string was assigned to it. Why has std::any been introduced? I think this is violating the least astonishment rule , because I find it hard to think of a situation, where this can be used to express more clearly,

How do I solve “template argument deduction/substitution failure” when implementing multi-type arithmetic operators

半城伤御伤魂 提交于 2020-01-25 04:15:26
问题 I can't seem to find the correct way to implement it, this seems to be the closest to the right way but I am getting a template argument deduction error. Can anyone point out where I went wrong? I am trying to add arithmetic capabilities to std::variant without needing to std::get first: #include <iostream> #include <variant> template<typename... Types> class variant : public std::variant<Types...> { private: template <class Op, typename T, int index = 0> decltype(auto) calc(const T& other)

Errors when using constexpr-if: expected '(' before 'constexpr'

风流意气都作罢 提交于 2020-01-24 11:53:32
问题 I am trying to use if-constexpr to check something, but I encounter errors like expected '(' before 'constexpr' 'else' without a previous 'if' " So far i check there is nothing wrong with my codes My compiling flag is g++ -std=c++17 main.cpp #include <iostream> template<typename T, typename Comp = std::less<T> > struct Facility { template<T ... list> struct List { static void print() { std::cout<<"\""<<"Empty List"<<"\""<<"\n"; } }; template<T head,T ... list> struct List<head,list...> {

Errors when using constexpr-if: expected '(' before 'constexpr'

爷,独闯天下 提交于 2020-01-24 11:52:36
问题 I am trying to use if-constexpr to check something, but I encounter errors like expected '(' before 'constexpr' 'else' without a previous 'if' " So far i check there is nothing wrong with my codes My compiling flag is g++ -std=c++17 main.cpp #include <iostream> template<typename T, typename Comp = std::less<T> > struct Facility { template<T ... list> struct List { static void print() { std::cout<<"\""<<"Empty List"<<"\""<<"\n"; } }; template<T head,T ... list> struct List<head,list...> {

Can the types of parameters in template functions be inferred?

时光毁灭记忆、已成空白 提交于 2020-01-24 10:47:04
问题 I'm writing some template functions in C++, but I'm not sure if it's possible to define a template function that infers the types of its parameters. I tried to define a template with inferred parameter types, but this example won't compile: template <auto> auto print_stuff(auto x, auto y) { std::cout << x << std::endl; std::cout << y << std::endl; } It works when I give a unique name to each parameter type, but this seems somewhat redundant: #include <iostream> #include <string> template

Why is the dynamic_cast allowed to yield a null-pointer for polymorphic classes when the destination pointer is not of the type of a base class?

陌路散爱 提交于 2020-01-24 09:00:11
问题 Consider the following program #include <iostream> #include <iomanip> struct A { }; struct C { }; int main() { C *pc = nullptr; A *pa1 = dynamic_cast<A *>( pc ); std::cout << "pa1 == nullptr is " << std::boolalpha << ( pa1 == nullptr ) << '\n'; A *pa2 = pc; std::cout << "pa2 == nullptr is " << std::boolalpha << ( pa2 == nullptr ) << '\n'; } For the both pointer declarations, pa1 and pa2, the compiler reports an error that such an initialization is not allowed. For example the clang HEAD 10.0

Get a std::tuple element as std::variant

∥☆過路亽.° 提交于 2020-01-24 05:11:04
问题 Given a variant type: using Variant = std::variant<bool, char, int, float, double, std::string>; and a tuple type containing elements restricted to this variant types (duplicates and omissions are possible, but no additional types): using Tuple = std::tuple<char, int, int, double, std::string>; How to implement methods that gets and sets a tuple element by a given index as Variant at runtime: Variant Get(const Tuple & val, size_t index); void Set(Tuple & val, size_t index, const Variant &

Random number generator performance varies between platforms

亡梦爱人 提交于 2020-01-23 17:39:06
问题 I am testing the performance of random number generators in c++ and have come upon some very strange results that I do not understand. I have tested std::rand vs std::uniform_real_distribution which uses std::minstd_rand. Code for timing std::rand auto start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < 1000000; ++i) std::rand(); auto finish = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> elapsed = finish - start; std::cout << "Elapsed time: " <<