c++14

Perfect forwarding for functions inside of a templated C++ class

穿精又带淫゛_ 提交于 2021-02-16 20:32:47
问题 Is there a good way to get perfect forwarding for functions inside of a templated class? Specifically, in the code #include <iostream> // Forward declare a Bar struct Bar; // Two different functions that vary based on the kind of argument void printme(Bar const & bar) { std::cout << "printme: constant reference bar" << std::endl; } void printme(Bar && bar) { std::cout << "printme: r-value reference bar" << std::endl; } void printme2(Bar const & bar) { std::cout << "printme2: constant

Boost variant visitor with an extra parameter

烈酒焚心 提交于 2021-02-15 20:51:04
问题 I have code that resembles below. typedef uint32_t IntType; typedef IntType IntValue; typedef boost::variant<IntValue, std::string> MsgValue; MsgValue v; Instead of saying this, IntValue value = boost::apply_visitor(d_string_int_visitor(), v); I would like to pass an extra parameter like this: But operator() gives a compile error. //This gives an error since the overload below doesn't work. IntValue value = boost::apply_visitor(d_string_int_visitor(), v, anotherStr); class d_string_int

Why does shared_ptr<T>::use_count() return a long instead of an unsigned type?

别来无恙 提交于 2021-02-15 12:07:38
问题 shared_ptr observers 20.8.2.2.5 C++14 Final Draft (n4296) long use_count() const noexcept; Returns: the number of shared_ptr objects, *this included, that share ownership with *this , or 0 when *this is empty. [Note: use_count() is not necessarily efficient. — end note] 回答1: According to this page http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1450.html The return type of use_count is signed to avoid pitfalls such as p.use_count() > -1 evaluating to false. with a reference to John

Best way to specialize function using enable_if for only some types

最后都变了- 提交于 2021-02-11 17:45:42
问题 I have this code that specializes the print function for two types, and reverts back to the base version for any other types. My question is, is there a way to write this without having to type out the negative case for all the specialized versions in the enable_if of the base print function? i.e. is there a way to remove all the !std::is_same and still have an unambiguous print function? Any versions of C++ welcome, but one that works in c++14 would be helpful. #include <iostream> template

Best way to specialize function using enable_if for only some types

你说的曾经没有我的故事 提交于 2021-02-11 17:45:22
问题 I have this code that specializes the print function for two types, and reverts back to the base version for any other types. My question is, is there a way to write this without having to type out the negative case for all the specialized versions in the enable_if of the base print function? i.e. is there a way to remove all the !std::is_same and still have an unambiguous print function? Any versions of C++ welcome, but one that works in c++14 would be helpful. #include <iostream> template

making threads C2672 and C2893 error code ( c++ )

百般思念 提交于 2021-02-11 16:51:24
问题 I have trouble compiling this code i get the following messages : C2672 'std::invoke': no matching overloaded function found C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept()' static auto f = [ ] ( int offset , int step , std::vector<Vert>& vertices , const Transform &transform ) { // do stuff }; // create threads int max_threads = 4 ; std::thread **active_threads = new std::thread * [ max_threads + 1 ] ; for ( int i = 0 ; i < max

asio::io_service is ending immediately with work

亡梦爱人 提交于 2021-02-10 05:12:38
问题 I'm trying to learn io_service and work with shared pointers. I want the code to work infinitely until I call stop method or sth like this. Unfortunately after seeing workHandler's output on the screen the program shutdowns. Can anybody explain why this happen? #include <boost/asio.hpp> #include <iostream> #include <atomic> #include <memory> #include <thread> #include <vector> class Service : public std::enable_shared_from_this<Service> { std::shared_ptr<boost::asio::io_service> _service; std

braced-init-list and unsigned types

这一生的挚爱 提交于 2021-02-08 13:23:17
问题 gcc 8 and clang 7 do not accept the following code, which should default-construct a temporary of type unsigned int : unsigned int ui = unsigned int{}; clang 7 reports an error such as <source>:6:22: error: expected primary-expression before 'unsigned' Visual C++ 2015 and 2017 accept this. Obviously, this works with a type such as int , or any default-constructible class type. Is this correct C++14 code (and in that case a bug of clang and gcc)? If not, why not? Which types other than

braced-init-list and unsigned types

白昼怎懂夜的黑 提交于 2021-02-08 13:22:41
问题 gcc 8 and clang 7 do not accept the following code, which should default-construct a temporary of type unsigned int : unsigned int ui = unsigned int{}; clang 7 reports an error such as <source>:6:22: error: expected primary-expression before 'unsigned' Visual C++ 2015 and 2017 accept this. Obviously, this works with a type such as int , or any default-constructible class type. Is this correct C++14 code (and in that case a bug of clang and gcc)? If not, why not? Which types other than

Using SFINAE to disable template class member function

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-08 13:22:09
问题 Is it possible to use SFINAE and std::enable_if to disable a single member function of a template class? I currently have a code similar to this: #include <type_traits> #include <iostream> #include <cassert> #include <string> class Base { public: virtual int f() { return 0; } }; template<typename T> class Derived : public Base { private: T getValue_() { return T(); } public: int f() override { assert((std::is_same<T, int>::value)); T val = getValue_(); //return val; --> not possible if T not