language-lawyer

Is using if (0) to skip a case in a switch supposed to work?

你离开我真会死。 提交于 2020-12-24 02:43:15
问题 I have a situation where I would like for two cases in a C++ switch statement to both fall through to a third case. Specifically, the second case would fall through to the third case, and the first case would also fall through to the third case without passing through the second case. I had a dumb idea, tried it, and it worked! I wrapped the second case in an if (0) { ... } . It looks like this: #ifdef __cplusplus # include <cstdio> #else # include <stdio.h> #endif int main(void) { for (int i

c++ auto for type and nontype templates

久未见 提交于 2020-12-14 11:55:36
问题 In c++17 template <auto> allows to declare templates with arbitrary type parameters. Partially inspired by this question, it would be useful to have an extension of template <auto> that captures both type and nontype template parameters, and also allows for a variadic version of it. Are there plans for such an extension in the next c++20 release? Is there some fundamental problem in having a syntax like template<auto... X> , with X any type or nontype template parameter? 回答1: Are there plans

c++ auto for type and nontype templates

折月煮酒 提交于 2020-12-14 11:44:07
问题 In c++17 template <auto> allows to declare templates with arbitrary type parameters. Partially inspired by this question, it would be useful to have an extension of template <auto> that captures both type and nontype template parameters, and also allows for a variadic version of it. Are there plans for such an extension in the next c++20 release? Is there some fundamental problem in having a syntax like template<auto... X> , with X any type or nontype template parameter? 回答1: Are there plans

c++ auto for type and nontype templates

梦想的初衷 提交于 2020-12-14 11:42:24
问题 In c++17 template <auto> allows to declare templates with arbitrary type parameters. Partially inspired by this question, it would be useful to have an extension of template <auto> that captures both type and nontype template parameters, and also allows for a variadic version of it. Are there plans for such an extension in the next c++20 release? Is there some fundamental problem in having a syntax like template<auto... X> , with X any type or nontype template parameter? 回答1: Are there plans

If a function template has deduced return type, is there a way to call it without instantiating the definition?

﹥>﹥吖頭↗ 提交于 2020-12-08 07:45:32
问题 Consider some function template such as: template <class T> const auto& foo() { static T t; return t; } The definition would not be valid if T were to be void . Nonetheless, we are allowed to instantiate the declaration alone without triggering an error: extern template const auto& foo<void>(); // explicit instantiation declaration Now let's consider situations where foo is called, rather than being explicitly instantiated. Obviously, if foo is ever called in an evaluated context, the

Can a constexpr function contain a label?

依然范特西╮ 提交于 2020-12-08 07:10:35
问题 This program: constexpr void f() { x: ; } is compiled by gcc, but clang says: error: statement not allowed in constexpr function So is this code valid? 回答1: As pointed out in a comment, clang is correct, and the code is ill-formed. According to the current working draft (which includes C++20), dcl.constexpr#3 says: The definition of a constexpr function shall satisfy the following requirements: ... its function-body shall not enclose ... an identifier label, ... ... 来源: https://stackoverflow

Is it legal to explicitly specify a generic lambda's operator() template arguments?

亡梦爱人 提交于 2020-12-08 05:51:00
问题 Is the following C++ code standard compliant? #include <iostream> int main() { [](auto v){ std::cout << v << std::endl; }.operator()<int>(42); } Both clang++ 3.8.0 and g++ 7.2.0 compile this code fine (the compiler flags are -std=c++14 -Wall -Wextra -Werror -pedantic-errors ). 回答1: This is indeed standard compliant. The standard specifies there must be a member operator() , and that it has one template argument for every occurence of auto in its paramater-declaration-clause. There is no

Does an explicit instantiation declaration of a member function of a class template cause instantiation of the class template?

穿精又带淫゛_ 提交于 2020-12-06 16:00:14
问题 [dcl.spec.auto]/14 states [ emphasis mine]: An explicit instantiation declaration does not cause the instantiation of an entity declared using a placeholder type , but it also does not prevent that entity from being instantiated as needed to determine its type. [  Example: template <typename T> auto f(T t) { return t; } extern template auto f(int); // does not instantiate f<int> int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit // instantiation definition

Does an explicit instantiation declaration of a member function of a class template cause instantiation of the class template?

浪子不回头ぞ 提交于 2020-12-06 15:55:26
问题 [dcl.spec.auto]/14 states [ emphasis mine]: An explicit instantiation declaration does not cause the instantiation of an entity declared using a placeholder type , but it also does not prevent that entity from being instantiated as needed to determine its type. [  Example: template <typename T> auto f(T t) { return t; } extern template auto f(int); // does not instantiate f<int> int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit // instantiation definition

Does an explicit instantiation declaration of a member function of a class template cause instantiation of the class template?

孤者浪人 提交于 2020-12-06 15:52:14
问题 [dcl.spec.auto]/14 states [ emphasis mine]: An explicit instantiation declaration does not cause the instantiation of an entity declared using a placeholder type , but it also does not prevent that entity from being instantiated as needed to determine its type. [  Example: template <typename T> auto f(T t) { return t; } extern template auto f(int); // does not instantiate f<int> int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit // instantiation definition