c++14

constexpr void function rejected

拜拜、爱过 提交于 2019-12-23 07:37:07
问题 I have this very simple function which won't compile. constexpr void func() { } The error I'm getting is: error: invalid return type ' void ' of constexpr function ' constexpr void func() ' constexpr void func() In C++14, void is a literal type [§3.9/10]: A type is a literal type if it is: void; or a scalar type; or a reference type; or an array of literal type; or a class type (Clause 9) that has all of the following properties: it has a trivial destructor, it is an aggregate type (8.5.1) or

constexpr array of constexpr objects using move ctor

泪湿孤枕 提交于 2019-12-23 07:31:09
问题 I have a class with a constexpr value constructor, but no copy or move ctor class C { public: constexpr C(int) { } C(const C&) = delete; C& operator=(const C&) = delete; }; int main() { constexpr C arr[] = {1, 2}; } I've found that this code doesn't work because it's actually trying to use the move constructor for C rather than the value constructor to construct in place. One issue is that I want this object to be unmovable (for test purposes) but I thought "okay, fine, I'll add a move

Forward declare a constexpr variable template

微笑、不失礼 提交于 2019-12-23 07:21:41
问题 I tried to forward-declare a constexpr variable template like this: template<typename> constexpr std::size_t iterator_category_value; The goal was to document that every specialization should be constexpr but I have to admit that I never checked whether it was legal or not and g++ was happy with it. However, when I tried to compile this spinnet with clang++ instead, I got the following error: error: default initialization of an object of const type 'const std::size_t' (aka 'const unsigned

Checking if a type is a map

寵の児 提交于 2019-12-23 07:17:09
问题 I sometimes find the need to write general routines that can be applied to a container of objects, or a map of such containers (i.e. process each container in the map). One approach is to write separate routines for map types, but I think it can be more natural and less verbose to have one routine that works for both types of input: template <typename T> auto foo(const T& items) { return foo(items, /* tag dispatch to map or non-map */); } What is a safe, clean way to do this tag dispatch? 回答1

C++ Assign to implicitly converted lvalue

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 07:02:27
问题 Consider this snippet of C++ code: struct Foo { float value; operator float& () { return this->value; } }; int main() { Foo foo; foo=1.0f; //Doesn't compile, foo isn't implicitly converted to a float& return 0; } Why doesn't this compile? Is there a specific reason this wasn't included in the C++ standard? Or an equivalent does indeed exist and I'm just using it wrong? 回答1: For pretty much all other operators, your conversion operator would do exactly what you want, and it would continue to

Why can the return type of main not be deduced?

守給你的承諾、 提交于 2019-12-23 06:44:49
问题 As expected, the following fails in C++11 because that language does not have return type deduction for bog standard functions: auto main() { return 0; } However, C++14 does, so I cannot explain the following error (with equivalent outcomes in GCC trunk, clang 3.8 and Visual Studio 2015): error: 'main' must return 'int' Is there a passage in the standard that I'm not seeing, forbidding return type deduction for main ? Or are both compilers non-compliant? (For what it's worth, I'd never

Truncate a String at Compile-Time

你离开我真会死。 提交于 2019-12-23 03:39:27
问题 I have a string literal with a value that is out of my control (for example a #define in a config.h file) and I want to initialize a global fixed-size character array with it. If the string is too long, I want it to be truncated. Basically, what I want to achieve is the effect of #define SOMETEXT "lorem ipsum" #define LIMIT 8 char text[LIMIT + 1]; std::strncpy(text, SOMETEXT, LIMIT); text[LIMIT] = '\0'; except that I cannot use this code because I want text to be a statically initialized

Is it enough to declare a function as transaction_safe, so they can be used thread-safe?

十年热恋 提交于 2019-12-23 02:39:13
问题 Is enough just to declare all of the functions as transaction_safe in some my class, so its can be used as thread-safe in transactions atomic_noexcept, atomic_cancel, atomic_commit from Experimental Transactional Memory TS? As known there are Transactional Memory TS (ISO/IEC TS 19841:2015) in the Experimental C++ standard libraries. Simple examples are here: http://en.cppreference.com/w/cpp/language/transactional_memory Also there is Technical Specification for C++ Extensions for

Capturing generic callable objects in nested lambdas - always forward?

有些话、适合烂在心里 提交于 2019-12-22 18:01:25
问题 I have various functions in my codebase that take a generic callable object and pass it to a series of nested lambdas before calling it. Example: template <typename TF> void interface(TF&& f) { nested0([/*...*/]() { nested1([/*...*/](auto& x) { nested2([&x, /*...*/]() { f(x); }); }); }); } Note that interface is taking a callable object of type TF by forwarding reference (previously known as universal reference) . The callable object is usually a lambda with various captured variables, both

Overloaded output operator not found for Boost.Spirit expression

人盡茶涼 提交于 2019-12-22 14:52:39
问题 This is a follow-up on this Q&A. I now have several data structures in a namespace ast , subdivided over two sub-namespaces ( algebraic and numeric ) that correspond to the two different formats that the grammar recognizes. namespace ast { namespace algebraic { struct occupance { char pc; char col; int row; }; using pieces = std::vector<occupance>; struct placement { char c; boost::optional<pieces> p; }; } namespace numeric { struct occupance { char pc; int sq; }; struct range { occupance oc;