c++17

Can static/dynamic/const/reinterpret_cast be used in unevaluated context?

我只是一个虾纸丫 提交于 2019-12-06 00:05:58
I tried to provide structures for checking is A is (choose cast)-castable to B . All four casts would have exact same implementation, expect their names (local-macro-able definitions would be possible, but not necessary). I wrote many check-for operators structures, for example: #include <iostream> #include <type_traits> #include <string> template<class, class, class, class = void> struct is_valid_ternary_operator : std::false_type { }; template<class T, class S, class R> struct is_valid_ternary_operator <T, S, R, std::void_t<decltype(std::declval<T>() ? std::declval<S>() : std::declval<R>())>

cleaning nullptr in one-to-many relation that use custom weak pointer

谁都会走 提交于 2019-12-05 23:40:10
问题 I have a one-to-many map class - MyMap1N<WeakPtr_Parent,WeakPtr_Children> . By design, it is supposed to store weak pointers of game-related instance. Roughly speaking, it is called like :- MyMap1N<WeakPtr<Room>,WeakPtr<RigidBody>> map; WeakPtr<Room> room=create<Room>(); WeakPtr<RigidBody> body=create<RigidBody>(); map.add(room,body); MyArray<WeakPtr<RigidBody>> bodys=map.getAllChildren(room); By profiling, I found that std::unordered_map is too slow. Thus, I had to find another way to

C++ standard: ODR and constexpr std::string_view

回眸只為那壹抹淺笑 提交于 2019-12-05 23:22:03
If I have a header foo.h which contains #ifndef FOO_H_ #define FOO_H_ namespace foo { constexpr std::string_view kSomeString = "blah"; } #endif // FOO_H_ then is it safe to include foo.h from within multiple .cc files in a single program, regardless of what they do with the symbol kSomeString , or are there some uses that could cause an ODR violation? Also, is it guaranteed that kSomeString.data() will return the same pointer across .cc files? I'd like specific references to wording in the C++ standard if possible. Thanks! Merely including foo.h from multiple translation units will not violate

C++ Shorten subsequent function calls

。_饼干妹妹 提交于 2019-12-05 23:15:21
I try to find a way in C++17 to shorten following function calls: GetCurrentContext()->GetEntityManager()->CreateEntity(); maybe to something like: EM()->CreateEntity(); I tried a function pointer, but this is only valid for static functions: constexpr auto &EM = GetContext()->GetEntityManager; // error: reference to non-static member function must be called; Is there a better solution than using a Macro? #define EM GetContext()->GetEntityManager When using an inline function call, i'm afraid, that the compiler will ignore this and i have an unnessessary overhead: inline EntityManager* EM() {

::std::initializer_list vs variadic templates

岁酱吖の 提交于 2019-12-05 22:46:51
Does passing multiple arguments via ::std::initializer_list offer any advantages over the variadic function template method? In code: template <typename T> void f(::std::initializer_list<T> const); template <typename ...A> void f(A&& ...args); Note that the types A... can be restricted to a single type as well, through SFINAE or static_assert() . The arguments can be iterated through ... You can iterate over an std::initializer_list in a way that you cannot over a parameter pack (unless you have C++17 and you fold it first). So for example you can have for loops with the lists. With parameter

What type will make “std::has_unique_object_representations” return false?

若如初见. 提交于 2019-12-05 21:46:08
问题 At cppref, I see a weird type trait checker : std::has_unique_object_representations From its description, I cannot imagine any type T that make std::has_unique_object_representations<T>::value is false . Is there any counter-example? 回答1: Understanding the purpose of this trait requires understanding the distinction between an objects "value representation" and its "object representation". From the standard: The object representation of an object of type T is the sequence of N unsigned char

Iterating over tuple in C++17/20 [duplicate]

南楼画角 提交于 2019-12-05 19:38:49
This question already has an answer here: How can you iterate over the elements of an std::tuple? 18 answers Does anyone know of a good, clean way to iterate over a tuple in C++17 / 20? Let's say we have a bit of code like this: class Test { public: Test( int x ) : x_(x) {}; void Go() const { std::cout << "Hi!" << x_ << "\n" ; } int x_; }; int main() { std::tuple tplb{ Test{1} , Test{2} , Test{3} }; } How could we iterate through the tuple and call the Go() method on each using the latest 17/20 features? I know you could just have a vector of the object and then it works easily. My goal with

Call a functor with a specific function from an overload set

余生长醉 提交于 2019-12-05 19:34:35
Context In mathematics-related context, I'd like to define functors working on <cmath> functions. For the purpose of this question, we will be using std::invoke as our functor. This is ill-formed (live demo) : std::invoke(std::sin, 0.0); (g++-8.1) error: no matching function for call to 'invoke(<unresolved overloaded function type>, double)' Indeed, std::sin is an overload set and the compiler lacks the type information to choose one of those functions. Question How could I name a specific function from an overload set? With what could we replace LEFT and RIGHT so that the following is well

How to write a template wrapper method for other class member functions?

拥有回忆 提交于 2019-12-05 18:52:54
I try to create a templated wrapper for different functions with different parameters. The setup is a class A with the basic implementation of two methods foo and bar . Another class B shall wrap these methods and add new functionality. The solution from the following link works very well for non-class functions: c++11: Templated wrapper function But if I try to invoke methods from another class I get an error. #include <algorithm> #include <functional> #include <iostream> class A { public: void foo(int x) { std::cout << "Foo: " << x << std::endl; } void bar(int x, float y) { std::cout << "Bar

Unable to take same type in std::variant

让人想犯罪 __ 提交于 2019-12-05 18:47:21
I'm using c++17, and would like to write the code something like this, #include <variant> typedef int NewInt; int main() { std::variant<NewInt, int> n = 1; } But it emits compile error, po.cpp: In function ‘int main()’: po.cpp:5:35: error: conversion from ‘int’ to non-scalar type ‘std::variant<int, int>’ requested std::variant<NewInt, int> n = 1; ^ How could I define the type like std::variant<NewInt, int> or is it impossible? A type alias is just another name for an existing type, not a new type. So you have a variant of two ints. And while it's allowed, you must address the ambiguity