c++14

constexpr errors; VS2017 C++ compiler regression?

别来无恙 提交于 2019-12-04 11:45:17
问题 Just installed VS2017, which claims to have superior C++14 support since 2015 (which was rudimentary). Gave it a spin on one of my projects which uses constexpr , and noticed what appear to be some regressions. This code: struct s { size_t i; constexpr s(nullptr_t) noexcept : i(0) {} }; static_assert(s(nullptr).i == 0, "!!"); Compiles no problem on VS2015 and Clang, but I get a new error in VS2017: error C2131: expression did not evaluate to a constant note: failure was caused by unevaluable

Directly write into char* buffer of std::string

那年仲夏 提交于 2019-12-04 11:42:48
问题 So I have an std::string and have a function which takes char* and writes into it. Since std::string::c_str() and std::string::data() return const char* , I can't use them. So I was allocating a temporary buffer, calling a function with it and copying it into std::string . Now I plan to work with big amount of information and copying this buffer will have a noticeable impact and I want to avoid it. Some people suggested to use &str.front() or &str[0] but does it invoke the undefined behavior?

How to track memory assign by STL library

不羁岁月 提交于 2019-12-04 11:41:16
I want to track all the memory(size allocated by std lib) allocated by all STL containers like map,list,vector etc. I just want to track STL container not regular object creation. Basically want to override new and delete of std lib. Example class demo { public: int i; std::list<int> mylist; } int main() { demo dd = new demo(); // -> Don't want to track this. Just want to track // mylist(size of my list) } I found out that std has it's own allocator option. For example list has it is allocator template < class T, class Alloc = allocator<T> > class list; What is the default allocator if I don't

Type traits to check if class has member function

风流意气都作罢 提交于 2019-12-04 11:24:25
问题 Trying to create a way to identify if a given class has a given function that can be invoked, and returns some type. Any idea on what I'm doing wrong here? Is there a better way to determine if a given method is invoke'able given a class? #include <string> #include <type_traits> #define GENERATE_HAS_MEMBER_FUNC(func, rettype) \ template<typename T, class Enable = void> struct has_##func; \ template<typename T, class U> struct has_##func : std::false_type {}; \ template<typename T> \ struct

Create OpenGL Context only with XCB

こ雲淡風輕ζ 提交于 2019-12-04 10:14:28
I want to create OpenGL Context only with XCB without GLX and Xlib. Could you tell me that might be happened. Thanks a lot. I want to create OpenGL Context only with XCB without GLX and Xlib. You can't. Period. You need GLX, because that's how the X11 does OpenGL. And GLX is written against Xlib. The best you could do is use the Xcb GLX module to implement a purely indirect GLX context; limits you to OpenGL-2.1 though and everything has to pass through the X11 server, instead of having a direct context that talks directly to the GPU. 来源: https://stackoverflow.com/questions/32756336/create

Why I can not return initializer list from lambda

喜欢而已 提交于 2019-12-04 09:57:28
问题 Why this code is not valid? auto foo=[](){ return {1,2}; }; However, this is valid since the initializer list is used just to initialize a vector not to return itself: auto foo=[]()->std::vector<int>{ return {1,2}; }; Why I can not return initializer list ? It could be useful. For example, a lambda that can be used to initialize a vector or a list or ... with some default values for something. 回答1: Lambda return type deduction uses the auto rules, which normally would have deduced std:

Generic lambda with std::function does not capture variables

流过昼夜 提交于 2019-12-04 09:57:02
问题 I'm trying to use the generic lambda of C++14, but got a trouble with std::function. #include <iostream> #include <functional> int main() { const int a = 2; std::function<void(int)> f = [&](auto b) { std::cout << a << ", " << b << std::endl; }; f(3); } This fails to compile with an error message saying that error: ‘a’ was not declared in this scope . It works if I change it to (int b) . Is it a bug? or am I missing something? The version of GCC i'm using is 4.9.2. 回答1: I can reproduce this

Function to Lambda

南笙酒味 提交于 2019-12-04 09:48:03
问题 It's often useful in C++ to work with lambdas and function objects, rather than function pointers. The reason being that a lambda's or function object's type fully encodes what is getting called (and thus allows inlining), whereas a function pointer's type merely encodes the signature. I had the interesting thought that it might be useful when using generic code to create a lambda that calls a given function. In other words, a higher order function that given a specified function, returns a

C++ Convert a parameter pack of types to parameter pack of indices

戏子无情 提交于 2019-12-04 09:38:55
Is there any way to convert a parameter pack of types to a parameter pack of integers from 0 to sizeof...(Types) ? More specifically, I'm trying to do something this this: template <size_t... I> void bar(); template <typename... Types> void foo() { bar<WHAT_GOES_HERE<Types>...>(); } For example, foo<int,float,double>() should call bar<0, 1, 2>() ; In my use case the parameter pack Types may contain the same type multiple times, so I cannot search the pack to compute the index for a given type. In C++14 you can use std::index_sequence_for from the <utility> header along with tagged dispatch.

Danger with virtual base move assignment operators when they are now allowed to be used?

好久不见. 提交于 2019-12-04 09:38:10
This concerns the resolution of C++ Issue http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1402 . Summary: template<typename T> struct wrap { wrap() = default; wrap(wrap&&) = default; wrap(const wrap&) = default; T t; }; struct S { S(){} S(const S&){} S(S&&){} }; typedef wrap<const S> W; // Error, defaulted move constructor of "wrap<const S>" is deleted! W get() { return W(); } (The issue is that we are getting an error for this snippet, even though the compiler could simply use the copy constructor of "S", as it does when the user explicitly writes the move constructor of "wrap".