c++14

Avoid if-else branching in string to type dispatching

孤者浪人 提交于 2019-12-06 16:15:39
问题 Usually when you write a CLI tool which accepts parameter you have to deal with them. Most of the time you want to switch between behaviours based on the value of an argument. The following is a common use case, where the program accepts a type and then prints something based on that type. I am using Boost to pre-process and auto generate the whole if-else branches. This is very nice in terms of maintainability as I only need to update a define when I introduce a new type. On the other hand

Static template member function for template class

旧城冷巷雨未停 提交于 2019-12-06 16:15:07
I have a template class and a template member function: template<class T1> struct A{ template<class T2> static int f(){return 0;} }; I want to specialize for a case when T1 and T2 are the same, For example, define the case A<T>::f<T> for any T . but I can't find the combination of keywords to achieve this. How can I partially (?) specialize a combination of template class and a template static function? These are my unsuccessful attempts, and the error messages: 1) Specialize inside the class: fatal error: cannot specialize a function 'f' within class scope ) template<class T1> struct A{

Disallow the use of auto for a given class, C++14 vs. C++17 update

谁说我不能喝 提交于 2019-12-06 15:31:25
问题 What is the feature that allows me use auto for non-copyable (and non-movable) types in C++17 and didn't for C++14? Consider the following code: struct A{ A(A const&)=delete; A(A&&)=delete; }; int main(){ auto a1 = A{}; // ok in C++17, not ok in C++14 auto&& a2 = A{}; // ok in C++17, ok in C++14 } It turns out that this was invalid code in C++14 but it is valid in C++17. The behavior is consistent in clang and gcc: https://godbolt.org/z/af8mEc The reason I ask is because until recently I was

Searching through a tuple for arguments of a function

天涯浪子 提交于 2019-12-06 14:56:44
Consider this int foo (int a, char c, bool b) {std::cout << a << ' ' << c << ' ' << b << '\n'; return 8;} double bar (int a, char c, bool b, int d) {std::cout << a << ' ' << c << ' ' << b << ' ' << d << '\n'; return 2.5;} char baz (bool a, bool b) {std::cout << a << ' ' << b << '\n'; return 'a';} int main() { const auto tuple = std::make_tuple(5, true, 'a', 3.5, false, 1000, 't', 2, true, 5.8); const std::tuple<int, double, char> t = searchArguments (tuple, foo, bar, baz); } So the arguments for foo are first searched (from tuple ). Searching from left to right, the first int found is 5 , the

Capturing generic callable objects in nested lambdas - always forward?

可紊 提交于 2019-12-06 14:04:45
I have various functions in my codebase that take a generic callable object and pass it to a series of nested lambdas before calling it. Example: template <typename TF> void interface(TF&& f) { nested0([/*...*/]() { nested1([/*...*/](auto& x) { nested2([&x, /*...*/]() { f(x); }); }); }); } Note that interface is taking a callable object of type TF by forwarding reference (previously known as universal reference) . The callable object is usually a lambda with various captured variables, both by value and by reference. What is the best (in terms of performance) way of capturing f in the nested

Local Static variable initialization is thread safe [duplicate]

梦想与她 提交于 2019-12-06 13:52:21
This question already has answers here : Is local static variable initialization thread-safe in C++11? [duplicate] (2 answers) Closed 3 years ago . Suppose I have a class with three static functions like this : #include <vector> #include <iostream> using namespace std; #include <thread> class Employee { }; class client { public: void Doprocessing() { //is this thread safe in c++11/14 static int i = CreateEmployee(); //does it look good to use static variable like this to make it thread safe? static int k = ProcessEmploye(); } private: static int CreateEmployee() { static Employee * e = new

Returning a class from a constexpr function requires virtual keyword with g++

。_饼干妹妹 提交于 2019-12-06 13:26:53
Hi the following program works with g++ 4.9.2 (Ubuntu 4.9.2-10ubuntu13) but the virtual keyword is required for the function get : //g++ -std=c++14 test.cpp //test.cpp #include <iostream> using namespace std; template<typename T> constexpr auto create() { class test { public: int i; virtual int get(){ return 123; } } r; return r; } auto v = create<int>(); int main(void){ cout<<v.get()<<endl; } If I omit the virtual keyword, I get the following error : test.cpp: In instantiation of ‘constexpr auto create() [with T = int]’: test.cpp:18:22: required from here test.cpp:16:1: error: body of

Dynamically switching symbol tables in x3

六眼飞鱼酱① 提交于 2019-12-06 12:47:56
问题 Given the following x3 grammar that parses correctly, I want to add validation of parameters, qualifiers, and properties. This would seem to indicate some method of dynamically switching which symbol table is being used within the various rules. What is the best way to implement this? It would seem to be some mixture of semantic actions and attributes, but it is not clear to me how. #include <string> #include <vector> #include <iostream> #include <iomanip> #include <map> #include <boost

Boost.Hana: How to check if function has specialisation for a certain type?

依然范特西╮ 提交于 2019-12-06 12:23:06
问题 I have a template function that has no definition by default but it specialised by some types: template <typename T> auto foo(bar &, const T &) -> void; template <> auto foo<std::string>(bar &, const std::string &) -> void {} How do I write a constexpr function that tells me if type T has a specialisation for the above function? My best effort: namespace detail { auto has_foo(hana::is_valid([](auto &b, const auto &t) -> decltype(foo(b, t)) {})); } // namespace detail template <typename T>

How to instantiate a member function pointer?

馋奶兔 提交于 2019-12-06 12:07:57
Lets say I have the follow code template<class MemberFunc> class Foo { MyClass object_; void call() { auto ptr = MemberFunc{}; (object_.*ptr)(); } }; int main() { Foo<decltype(&MyClass::doThings)> foo; foo.call(); } This code does crash for me because ptr is 0. Why does the member function constructor returns 0? My workaround is the following but it involves code duplication. Is there no other way to just construct/instantiate the member function from the type? C++14 welcome. template<class MemberFunc, MemberFunc f> class Foo { MyClass object_; void call() { (object_.*f)(); } }; int main() {