c++20

Invoking `constexpr` member function through reference - clang vs gcc

梦想的初衷 提交于 2020-03-18 05:03:09
问题 Consider the following example ( snippet (0) ): struct X { constexpr int get() const { return 0; } }; void foo(const X& x) { constexpr int i = x.get(); } int main() { foo(X{}); } The above example compiles with all versions of g++ prior to g++ 10.x , and never compiled under clang++ . The error message is: error: 'x' is not a constant expression 8 | constexpr int i = x.get(); | live example on godbolt.org The error kind of makes sense, as x is never a constant expression in the body of foo ,

if constexpr and requires-expression for ad-hoc concepts checking

北城余情 提交于 2020-03-18 04:30:10
问题 Let's say, given C++17's if constexpr and Concepts TS (for instance, in recent gcc versions), we'd like to check if a type in a template function has a nested type: #include <iostream> struct Foo { using Bar = int; }; template<typename T> void doSmth(T) { if constexpr (requires { typename T::Bar; }) std::cout << "has nested! " << typename T::Bar {} << std::endl; else std::cout << "no nested!" << std::endl; } int main() { doSmth(Foo {}); //doSmth(0); } The documentation for concepts is scarce,

Can there be different implicit objects based on a later runtime decision in C++20?

回眸只為那壹抹淺笑 提交于 2020-03-18 03:52:30
问题 This question refers to the addition of P0593 to the latest C++20 draft . Here is my example: #include <cstdlib> #include <cstdio> void foo(void *p) { if ( std::getchar() == 'i' ) { *(int *)p = 2; std::printf("%d\n", *(int *)p); } else { *(float *)p = 2; std::printf("%f\n", *(float *)p); } } int main() { void *a = std::malloc( sizeof(int) + sizeof(float) ); if ( !a ) return EXIT_FAILURE; foo(a); // foo(a); [2] } Is this code well-defined for all inputs under the latest draft? The rationale

Templated requires-expression

让人想犯罪 __ 提交于 2020-03-16 07:36:06
问题 I want a Functor concept in C++20. A functor is a higher-kinded type that can be mapped over. A simple example is std::optional ; with a function from type A to type B and a std::optional<A> , you can easily create a std::optional<B> by applying the function to the value if it exists and returning an empty optional otherwise. This operation is called fmap in Haskell. template<typename A, typename B> std::optional<B> fmap(std::function<B(A)> f, std::optional<A> fa) { if (!fa) { return std:

auto as a template argument placeholder for a function parameter

三世轮回 提交于 2020-03-14 05:42:19
问题 C++20 allows using auto for function parameter type. Does it also allow using auto as a template argument placeholder (not similar, but in the spirit of C++17 template<auto> in a way) for function parameter type? So the following code, pre C++20: template<typename First, typename Second> void printPair(const std::pair<First, Second>& p) { std::cout << p.first << ", " << p.second; } Could be written as: void printPair(const std::pair<auto, auto>& p) { std::cout << p.first << ", " << p.second;

auto as a template argument placeholder for a function parameter

谁说我不能喝 提交于 2020-03-14 05:41:27
问题 C++20 allows using auto for function parameter type. Does it also allow using auto as a template argument placeholder (not similar, but in the spirit of C++17 template<auto> in a way) for function parameter type? So the following code, pre C++20: template<typename First, typename Second> void printPair(const std::pair<First, Second>& p) { std::cout << p.first << ", " << p.second; } Could be written as: void printPair(const std::pair<auto, auto>& p) { std::cout << p.first << ", " << p.second;

Why is operator!= removed in C++20 for many standard library types?

折月煮酒 提交于 2020-03-13 03:50:42
问题 According to cppreference, std::type_info::operator!= gets removed with C++20, however, std::type_info::operator== apparently remains. What's the reasoning behind? I might agree on comparing for inequality being meaningless, but then comparing for equality would be just as meaningless as well, wouldn't it? Similarly, operator!= of many other standard library types, including containers such as std::unordered_map::operator!= and std::unordered_set::operator!= will be removed in C++20 according

Why is operator!= removed in C++20 for many standard library types?

僤鯓⒐⒋嵵緔 提交于 2020-03-13 03:49:54
问题 According to cppreference, std::type_info::operator!= gets removed with C++20, however, std::type_info::operator== apparently remains. What's the reasoning behind? I might agree on comparing for inequality being meaningless, but then comparing for equality would be just as meaningless as well, wouldn't it? Similarly, operator!= of many other standard library types, including containers such as std::unordered_map::operator!= and std::unordered_set::operator!= will be removed in C++20 according

Why is operator!= removed in C++20 for many standard library types?

偶尔善良 提交于 2020-03-13 03:48:49
问题 According to cppreference, std::type_info::operator!= gets removed with C++20, however, std::type_info::operator== apparently remains. What's the reasoning behind? I might agree on comparing for inequality being meaningless, but then comparing for equality would be just as meaningless as well, wouldn't it? Similarly, operator!= of many other standard library types, including containers such as std::unordered_map::operator!= and std::unordered_set::operator!= will be removed in C++20 according

Will consteval functions allow template parameters dependent on function arguments?

孤街浪徒 提交于 2020-03-10 10:50:02
问题 In C++17, this code is illegal: constexpr int foo(int i) { return std::integral_constant<int, i>::value; } That's because even if foo can be evaluated at compile-time, the compiler still needs to produce the instructions to execute it at runtime, thus making the template instantiation impossible. In C++20 we will have consteval functions, which are required to be evaluated at compile-time, so the runtime constraint should be removed. Does it mean this code will be legal? consteval int foo(int