c++14

Static templated constexpr nested class member

☆樱花仙子☆ 提交于 2019-12-20 02:15:47
问题 I have the following sample class Foo with nested class Bar and everything is constexpr : class Foo { private: template <typename T> struct Bar { constexpr Bar(){} constexpr int DoTheThing() const { return 1; } }; public: constexpr static auto b = Bar<int>{}; constexpr Foo() {} constexpr int DoTheThing() const { return b.DoTheThing(); } }; And I want to test that calling Foo::DoTheThing returns 1: int main() { constexpr Foo f; static_assert(f.DoTheThing() == 1, "DoTheThing() should return 1")

type defined in free function, accessible through auto outside. Language Bug or Feature?

删除回忆录丶 提交于 2019-12-20 01:55:08
问题 Let's define a class inside a free function, and access it outside: #include <iostream> auto myFunc(){ class MyType{public: int i = 0; int j = 1;}; return MyType(); } int main() { auto my_type = myFunc(); std::cout << my_type.i << " " << my_type.j << "\n"; return 0; } It compiles, run as expected: 0 1 The name MyType is properly hidden: if we replace auto, the following won't compile: int main() { MyType my_type = myFunc(); std::cout << my_type.i << " " << my_type.j << "\n"; return 0; } What

What is a good alternative to this C++17 fold expression in C++14?

余生长醉 提交于 2019-12-19 21:47:46
问题 Here is a nice, succinct fold expression based lambda in C++17: #include <cstdint> using ::std::uint64_t; constexpr auto sumsquares = [](auto... n) { return ((n * n) + ...); }; // I want this to work. uint64_t foo(uint64_t x, uint64_t y, uint64_t z) { return sumsquares(x, y, z); } // And this too double bar(uint64_t x, double y) { return sumsquares(x, y); } I have this code I've written to do something similar in C++14, but it seems a lot more verbose and confusing than it should be. I'm

What is a good alternative to this C++17 fold expression in C++14?

☆樱花仙子☆ 提交于 2019-12-19 21:47:19
问题 Here is a nice, succinct fold expression based lambda in C++17: #include <cstdint> using ::std::uint64_t; constexpr auto sumsquares = [](auto... n) { return ((n * n) + ...); }; // I want this to work. uint64_t foo(uint64_t x, uint64_t y, uint64_t z) { return sumsquares(x, y, z); } // And this too double bar(uint64_t x, double y) { return sumsquares(x, y); } I have this code I've written to do something similar in C++14, but it seems a lot more verbose and confusing than it should be. I'm

Why can't I use operator bool() for std::ofstream

回眸只為那壹抹淺笑 提交于 2019-12-19 17:42:41
问题 Why can't I write the following code? #include <fstream> #include <string> bool touch(const std::string& file_path) { return std::ofstream(file_path, std::ios_base::app); } int main() { touch("foo.txt"); } Output prog.cpp: In function 'bool touch(const string&)': prog.cpp:6:52: error: cannot convert 'std::ofstream {aka std::basic_ofstream<char>}' to 'bool' in return return std::ofstream(file_path, std::ios_base::app); http://ideone.com/IhaRaD I know that std::fstream 's operator bool()

Function argument returning void or non-void type

回眸只為那壹抹淺笑 提交于 2019-12-19 12:24:37
问题 I am in the middle of writing some generic code for a future library. I came across the following problem inside a template function. Consider the code below: template<class F> auto foo(F &&f) { auto result = std::forward<F>(f)(/*some args*/); //do some generic stuff return result; } It will work fine, unless I pass to it a function that returns void like: foo([](){}); Now, of course, I could use some std::enable_if magic to check the return type and perform specialization for a function

Function argument returning void or non-void type

别来无恙 提交于 2019-12-19 12:24:02
问题 I am in the middle of writing some generic code for a future library. I came across the following problem inside a template function. Consider the code below: template<class F> auto foo(F &&f) { auto result = std::forward<F>(f)(/*some args*/); //do some generic stuff return result; } It will work fine, unless I pass to it a function that returns void like: foo([](){}); Now, of course, I could use some std::enable_if magic to check the return type and perform specialization for a function

Strange MSVC behaviour with std::experimental::is_detected

ぐ巨炮叔叔 提交于 2019-12-19 10:17:42
问题 I implemented std::experimental::is_detected based on this article on cppreference.com (Part of the code is below + working repro). It works well on G++ and Clang++, but results in strange errornous behaviour with MSVC: is_detected seems to always be bool_constant<true> ! Here you can see the correct result using gcc 5.x : @ideone.com But with MSVC 19 (shipped with VS2015) the tests always succeed: Z:\>cl /EHsc test.cxx .... Z:\>test true, true So, is this a known bug in the compiler? Is it

How std::transform and std::plus work together?

≡放荡痞女 提交于 2019-12-19 09:51:14
问题 I was reading C++ reference and came across std::plus function with an example. Which is pretty straight forward, its simply adds lhs and rhs. The code was: #include <functional> #include <iostream> int main() { std::string a = "Hello "; const char* b = "world"; std::cout << std::plus<>{}(a, b) << '\n'; } output: Hello world I changed it to #include <functional> #include <iostream> int main() { int a = 5; int b = 1; std::cout << std::plus<int>{}(a, b) << '\n'; } output : 6 Now I made foo

Branching on constexpr evaluation / overloading on constexpr

可紊 提交于 2019-12-19 08:49:30
问题 The setup: I have a function that uses SIMD intrinsics and would like to use it inside some constexpr functions. For that, I need to make it constexpr. However, the SIMD intrinsics are not marked constexpr, and the constant evaluator of the compiler cannot handle them. I tried replacing the SIMD intrinsics with a C++ constexpr implementation that does the same thing. The function became 3.5x slower at run-time, but I was able to use it at compile-time (yay?). The problem : How can I use this