c++14

Unified way for checking the existence of member functions, free functions and operators

旧城冷巷雨未停 提交于 2019-12-24 15:51:55
问题 I have found several several questions on here that dealt with checking if either a member function, a free function or an operator exists for a given type. The proposed solutions solve the problem at hand but each uses a different approach. I am trying to find a way to deal with each of those problems in an identical or at least similar fashion. Checking if a Type C has a member func works: template<typename, typename T> struct has_member { static_assert( std::integral_constant<T, false>:

Efficient way to convert a compile time known function argument to a std::integral_constant

蓝咒 提交于 2019-12-24 15:11:05
问题 Yesterday I read a blog entry about converting a compile time known function argument from a constexpr function to a type like std::integral_constant<> . A possible usage example is to convert types from user defined literals. Consider the following example: constexpr auto convert(int i) { return std::integral_constant<int, i>{}; } void test() { // should be std::integral_constant<int, 22> using type = decltype(convert(22)); } But obviously and as expected Clang throws the following error:

why this variable isn't deduced as initializer_list in g++ in C++14?

拜拜、爱过 提交于 2019-12-24 15:04:54
问题 Consider the following program: #include <iostream> int main() { int n = 3; int fact = 1; for(auto i{1};i<=n;i++) fact*=i; std::cout<<"fact of "<<n<<" is "<<fact; } It compiles fine on ideone even when I use -std=c++14 option. See live demo here. But in C++14 the variable i should be deduced as initializer_list according to this. There is a proposal for C++1z that implements new type deduction rules for brace initialization: For direct list-initialization: For a braced-init-list with only a

How to keep the implicitly declared aggregate initialization constructor?

孤人 提交于 2019-12-24 13:39:43
问题 Suppose I have a class like this: struct X { char buf[10]; }; X x{"abc"}; This compiles. However, if I add user defined constructors to the class, this implicitly declared aggregate initialization constructor will be deleted: struct X { char buf[10]; X(int, int) {} }; X x{"abc"}; // compile error How can I declare that I want default aggregate initialization constructor, just like that for default constructor X()=default ? Thanks. 回答1: There is no "default aggregate initialization constructor

libxml2 xmlNodePtr's type changes when I observe it

半城伤御伤魂 提交于 2019-12-24 09:46:17
问题 I spent some time yesterday debugging a lovely Heisenbug where an xmlNodePtr 's type would change on me. This example shows the error: #include <iostream> #include <vector> #include <string> #include <memory> // std::unique_ptr #include <cstdint> #include <libxml/tree.h> #include <libxml/parser.h> struct SomeDataType { std::vector<std::vector<std::string>> data; explicit SomeDataType(uint32_t rows_, uint32_t columns_) : data(rows_) { for (uint32_t row = 0; row < rows_; ++row) { data[row]

Full example of using template comparator

假如想象 提交于 2019-12-24 09:39:52
问题 I am trying to a template class driven with a template parameter of std::less , std::greater or. This is a follow up to this question since, the answer doesn't provide a full example and I am unable to successfully use the template comparator. #include <functional> #include <algorithm> template <typename C> class Test { int compare(int l, int n, int x, int y) { public: bool z = C(x, y); if(l < n && z) { return 1; } else { return 2; } } }; int main() { Test<std::less<int>> foo; Test<std:

Undefined reference to `std::chrono::_V2::system_clock::now()' when linking with gfortran

坚强是说给别人听的谎言 提交于 2019-12-24 08:59:33
问题 I am trying to create an user defined block in INSEL which requires C++ to program and I link it using gfortran. I have the following code in my program // Setting seed for random number generators unsigned seed = static_cast<int> (std::chrono::system_clock::now().time_since_epoch().count()); to set as a seed to my random number generator. When i compile it using g++ (gcc v.5.1.0) it shows no error or warning. My compiling command is g++ -O0 -Wall -c -g3 -std=c++14 -fmessage-length=0 $

How to read unknown number of inputs?

允我心安 提交于 2019-12-24 08:58:47
问题 I am learning C++ using the book C++ Primer. In Section 1.4.3 , the following example code about reading the unknown number of inputs is given. #include <iostream> int main() { int sum = 0, value = 0; // read until end-of-file, calculating a running total of all values read while (std::cin >> value) sum += value; // equivalent to sum = sum + value std::cout << "Sum is: " << sum << std::endl; return 0; } According to the book, if we give an input of 3 4 5 6 , the output will be Sum is: 18 But

Unpack vector into arguments to call functions that have a variable number of arguments

守給你的承諾、 提交于 2019-12-24 08:05:34
问题 It's a little bit tricky trying to phrase this question. So let's say I have a large number of functions that all have varying numbers of arguments - some have none, some have one, some have a few or many more. I receive the parameters for these functions in a vector. Normally, I can just call them like this: #include <vector> int my_functionA(std::string a) { /*...*/ return 1; } int my_functionB(std::string a, std::string b) { /*...*/ return 2; } void some_useful_function(std::vector<std:

Why is `“literal”` encouraged to decay to `const char*` in C++ argument type match?

老子叫甜甜 提交于 2019-12-24 07:56:58
问题 I'm playing around with overloading operators in c++14, and I tried to match two types of arguments: any-old-const-char*, and a-string-literal. That is, I'm trying to see if I can discriminate between: const char * run_time; and "compile time" I wrote the code below, and as shown, when I try span >> "literal" it invoked the const char* function. When I #if 0 -out the const char* version, the template version gets called just fine. If I change the template version to take an rvalue-reference (