c++14

Why declare a deleted assignment operators with the ref-qualifier &

烂漫一生 提交于 2021-01-28 06:31:34
问题 As far as I know a user declared assignment operators differ from built-in operators, as explained by this stackoverflow answer. But why should one add the "&" to a deleted operator? // C++ class MyType { public: // ... MyType& operator=(MyType const&) & = delete; MyType& operator=(MyType &&) & noexcept = default; // more }; I ask because my static code checker reports a rule violation here and I see no reason to add the "&" for a deleted operator. Do I miss something? 回答1: There's no reason

Partial specialization failure with GCC

a 夏天 提交于 2021-01-28 06:03:45
问题 After answering this question I decided to dig deeper in the issue to find a minimal and replicable example with same error. Let assume I have the following primary template and I would like to specialize it to std::vector as follows: #include <vector> #include <iostream> template<typename T, typename T::value_type v> struct foo; template<typename T, T v> struct foo<std::vector<T>, v> { static constexpr T value = v; }; int main() { std::cout << foo<std::vector<int>, 32>::value << std::endl;

Partial specialization failure with GCC

泪湿孤枕 提交于 2021-01-28 05:56:35
问题 After answering this question I decided to dig deeper in the issue to find a minimal and replicable example with same error. Let assume I have the following primary template and I would like to specialize it to std::vector as follows: #include <vector> #include <iostream> template<typename T, typename T::value_type v> struct foo; template<typename T, T v> struct foo<std::vector<T>, v> { static constexpr T value = v; }; int main() { std::cout << foo<std::vector<int>, 32>::value << std::endl;

C++ - Can a template template parameter be of a variable or function?

ぃ、小莉子 提交于 2021-01-28 05:46:14
问题 I'm still trying to fully understand templates. I see them as special types. Recently I was reading about template template parameters of classes and I'm wondering if it it is possible to have a template template parameter of function or variable and not only of class? Something like this: template<typename T> void func(T); //template of function 'func' template<int a> double var = a; //template of variable 'var' template<template<typename> void templ_param() = func, //parameter template of

How to use the CPR library in c++?

感情迁移 提交于 2021-01-28 05:40:32
问题 I was previously developing in python and now I've switched to C++. I found a cool library called CPR https://github.com/whoshuu/cpr that can be used to make HTTP requests easily like python requests. Like in python there are no easy pkg managers like pip to install libraries in C++. How can I use cpr in my project. There are no dlls or lib file in that. 回答1: c++ packages are usually distributed as a set of development headers and static/shared libraries. However in the case of cpr , the

Partial Aggregate Initialization and Non-static Data Member Initializer

亡梦爱人 提交于 2021-01-27 02:30:34
问题 struct Point { int x = 0; int y = 10; }; Point p = {1,}; p.x == 1; // true p.y == 10; // is this true? According to the standard missing elements in initializer list are value initialized, so y should be int() or 0 , but it doesn't seem to say what happen in the situation of Non-static Data Member Initializer. Edit: According to the answer, apparently this is invalid c++11, I would like to know the situation in c++1y. 回答1: C++98, C++03 Non-static data member initialisers (NSDMIs) do not exist

Why 'constexpr' parameters are not allowed?

若如初见. 提交于 2021-01-27 01:50:15
问题 It would be useful to have 'constexpr' parameters in order to distinguish compiler-known values and so to be able detecting errors at compile-time. Examples: int do_something(constexpr int x) { static_assert(x > 0, "x must be > 0"); return x + 5; } int do_something(int x) { if(x > 0) { cout << "x must be > 0" << endl; exit(-1); } return x + 5; } int var; do_something(9); //instance 'do_something(constexpr int x)' and check arg validity at compile-time do_something(0); //produces compiler

Why constexpr is not the default for all function? [duplicate]

一个人想着一个人 提交于 2021-01-26 20:35:48
问题 This question already has answers here : Why do we need to mark functions as constexpr? (4 answers) Closed 4 years ago . After relaxing the rules for the constexpr it seems as these functions can be used everywhere. They can be called on both constant (constexpr) and local (mutable) variables as well. So for me it seem as just a hint for the compiler (like inline). I just keep writing it everywhere and remove it if compiler complains. So compiler seems to know everything if a function can be

Why constexpr is not the default for all function? [duplicate]

混江龙づ霸主 提交于 2021-01-26 20:34:35
问题 This question already has answers here : Why do we need to mark functions as constexpr? (4 answers) Closed 4 years ago . After relaxing the rules for the constexpr it seems as these functions can be used everywhere. They can be called on both constant (constexpr) and local (mutable) variables as well. So for me it seem as just a hint for the compiler (like inline). I just keep writing it everywhere and remove it if compiler complains. So compiler seems to know everything if a function can be

Black Magic using Initializer_list and pack expansion

不问归期 提交于 2021-01-22 14:40:19
问题 To expand flexible function parameters, there is a method using std::initializer_list . However I couldn't understand it. Can anyone explain this in an understandable way? template<typename T, typename... Args> auto print(T value, Args... args) { std::cout << value << std::endl; return std::initializer_list<T>{([&] { std::cout << args << std::endl; }(), value)...}; } 回答1: This is a very confused way of doing things, but C++14 requires that we do something similar. I'll explain the limitations