Are numeric_limits min/max constexpr? - c++

Does the C++11 standard specify that the numeric_limits<T>::min and max have to be constant-expression that can be used in templates or static_assert?
More generally, how to find the list of the functions that are constant-expression according to the standard?

Indeed the standard (or my latest working draft) lists all members of std::numeric_limits in chapter 18.3.2.3 [numeric.limits] as constexpr (it won't do any good to actually quote those definitions here), for the general templated version as well as all the builtin specializations (18.3.2.7 [numeric.special]). So yes, they are guaranteed to be constant expressions (for conforming implementations that also actually support constexpr, of course).
As to your second, more general, question I cannot help you that much except just refer you to the C++ standard itself, whose latest draft, which doesn't really differ from the actual standard, is available for free. Or you might look at the more convenient but less binding cppreference.com.

Related

Why is std::is_pod deprecated in C++20?

std::is_pod will be probably deprecated in C++20.
What's the reason for this choice? What should I use in place of std::is_pod to know if a type is actually a POD?
POD is being replaced with two categories that give more nuances. The c++ standard meeting in november 2017 had this to say about it:
Deprecating the notion of “plain old data” (POD). It has been replaced with two more nuanced categories of types, “trivial” and “standard-layout”. “POD” is equivalent to “trivial and standard layout”, but for many code patterns, a narrower restriction to just “trivial” or just “standard layout” is appropriate; to encourage such precision, the notion of “POD” was therefore deprecated. The library trait is_pod has also been deprecated correspondingly.
For simple data types use the is_standard_layout function, for trivial data types (such as simple structs) use the is_trivial function.

TS Concepts - advantages of concept with brackets [duplicate]

I've been looking at the C++1z N4377 Concepts TS draft that is being implemented in GCC 6, and I don't understand the purpose of having two different kinds of concepts: variable concepts and function concepts.
The relevant part of the draft for function concepts is [dcl.spec.concept (5.4)]
The declaration shall have a function-body equivalent to { return E; } where E is a constraint-expression (14.10.1.3).
and for variable concepts, in the next paragraph [(6.3)] :
The initializer shall be a constraint-expression.
Is there anything one of them can do, that the other cannot? If not, is there a rationale for including both?
Note: the latest draft, P0121R0 doesn't change anything in this regard
Function concepts can be overloaded on differing template parameter arity. Variable concepts provide nothing that function concepts don't, except for the ability to not put () in some contexts.
Eliminating variable concepts from the TS would not reduce the expressivity of concepts.

Why have both variable and function concepts in C++ Concepts TS?

I've been looking at the C++1z N4377 Concepts TS draft that is being implemented in GCC 6, and I don't understand the purpose of having two different kinds of concepts: variable concepts and function concepts.
The relevant part of the draft for function concepts is [dcl.spec.concept (5.4)]
The declaration shall have a function-body equivalent to { return E; } where E is a constraint-expression (14.10.1.3).
and for variable concepts, in the next paragraph [(6.3)] :
The initializer shall be a constraint-expression.
Is there anything one of them can do, that the other cannot? If not, is there a rationale for including both?
Note: the latest draft, P0121R0 doesn't change anything in this regard
Function concepts can be overloaded on differing template parameter arity. Variable concepts provide nothing that function concepts don't, except for the ability to not put () in some contexts.
Eliminating variable concepts from the TS would not reduce the expressivity of concepts.

Is std::vector<T> a `user-defined type`?

In 17.6.4.2.1/1 and 17.6.4.2.1/2 of the current draft standard restrictions are placed on specializations injected by users into namespace std.
The behavior of a C
++
program is undefined if it adds declarations or definitions to namespace
std
or to a
namespace within namespace
std
unless otherwise specified. A program may add a template specialization
for any standard library template to namespace
std
only if the declaration depends on a user-defined type
and the specialization meets the standard library requirements for the original template and is not explicitly
prohibited.
I cannot find where in the standard the phrase user-defined type is defined.
One option I have heard claimed is that a type that is not std::is_fundamental is a user-defined type, in which case std::vector<int> would be a user-defined type.
An alternative answer would be that a user-defined type is a type that a user defines. As users do not define std::vector<int>, and std::vector<int> is not dependent on any type a user defines, std::vector<int> is not a user-defined type.
A practical problem this impacts is "can you inject a specialization for std::hash for std::tuple<Ts...> into namespace std? Being able to do so is somewhat convenient -- the alternative is to create another namespace where we recursively build our hash for std::tuple (and possibly other types in std that do not have hash support), and if and only if we fail to find a hash in that namespace do we fall back on std.
However, if this is legal, then if and when the standard adds a hash specialization for std::tuple to namespace std, code that specialized it already would be broken, creating a reason not to add such specializations in the future.
While I am talking about std::vector<int> as a concrete example, I am trying to ask if types defined in std are ever user-defined type s. A secondary question is, even if not, maybe std::tuple<int> becomes a user-defined type when used by a user (this gets slippery: what then happens if something inside std defines std::tuple<int>, and you partial-specialize hash for std::tuple<Ts...>).
There is currently an open defect on this problem.
Prof. Stroustrup is very clear that any type that is not built-in is user-defined. See the second paragraph of section 9.1 in Programming Principles and Practice Using C++.
He even specifically calls out “standard library types” as an example of user-defined types. In other words, a user-defined type is any compound type.
Source
The article explicitly mentions that not everyone seems to agree, but this is IMHO mostly wishful thinking and not what the standard (and Prof. Stroustrup) are actually saying, only what some people want to read into it.
When Clause 17 says "user-defined" it means "a type not defined in the standard" so std::vector<int> is not user-defined, neither is std::string, so you cannot specialize std::vector<int> or std::vector<std::string>. On the other hand, struct MyClass is user-defined, because it's not a type defined in the standard, so you can specialize std::vector<MyClass>.
This is not the same meaning of "user-defined" used in clauses 1-16, and that difference is confusing and silly. There is a defect report for this, with some discussion recorded that basically says "yes, the library uses the wrong term, but we don't have a better one".
So the answer to your question is "it depends". If you're talking to a C++ compiler implementor or a core language expert, std::vector<int> is definitely a user-defined type, but if you're talking to a standard library implementor, it is not. More precisely, it's not user-defined for the purposes of 17,6.4.2.1.
One way to look at it is that the standard library is "user code" as far as the core language is concerned. But the standard library has a different idea of "users" and considers itself to be part of the implementation, and only things that aren't part of the library are "user-defined".
Edit: I have proposed changing the library Clauses to use a new term, "program-defined", which means something defined in your program (as opposed to UDTs defined in the standard, such as std::string).
As users do not define std::vector<int>, and std::vector<int> is not dependent on any type a user defines, std::vector<int> is not a user-defined type.
The logical counter argument is that users do define std::vector<int>. You see std::vector is a class template and as such has no direct representation in binary code.
In a sense it gets it binary representation through the instantiation of a type, so the very action of declaring a std::vector<int> object is what gives "soul" to the template (pardon the phrasing). In a program where noone uses a std::vector<int> this data type does not exist.
On the other hand, following the same argument, std::vector<T> is not a user defined type, it is not even a type, it does not exist; only if we want to (instantiate a type), it will mandate how a structure will be layed out but until then we can only argue about it in terms of structure, design, properties and so on.
Note
The above argument (about templates being not code but ... well templates for code) may seem a bit superficial but draws it's logic, from Mayer's introduction in A. Alexandrescu's book Modern C++ Design. The relative quote there, goes like this :
Eventually, Andrei turned his attention to the development of template-based implementations of popular language idioms and design patterns, especially the GoF[*] patterns. This led to a brief skirmish with the Patterns community, because one of their fundamental tenets is that patterns cannot be represented in code. Once it became clear that Andrei was automating the generation of pattern implementations rather than trying to encode patterns themselves, that objection was removed, and I was pleased to see Andrei and one of the GoF (John Vlissides) collaborate on two columns in the C++ Report focusing on Andrei's work.
The draft standard contrasts fundamental types with user-defined types in a couple of (non-normative) places.
The draft standard also uses the term "user-defined" in other contexts, referring to entities created by the programmer or defined in the standard library. Examples include user-defined constructor, user-defined operator and user-defined conversion.
These facts allow us, absent other evidence, to tentatively assume that the intent of the standard is that user-defined type should mean compound type, according to historical usage. Only an explicit clarification in a future standard document can definitely resolve the issue.
Note that the historical usage is not clear on types like int* or struct foo* or void(*)(struct foo****). They are compound, but should they (or some of them) be considered user-defined?

Why is there a sizeof... operator in C++0x?

I saw that #GMan implemented a version of sizeof... for variadic templates which (as far as I can tell) is equivalent to the built in sizeof.... Doesn't this go against the second design principle: prefer libraries to language extensions?
From Variadic Templates (Revision 3)
(N2080=06-0150), page 6:
Although not strictly necessary (we can implement count without this feature), checking the length of a parameter pack is a common operation that deserves a simple syntax. Moreover, this operation may become necessary for type-checking reasons when variadic templates are combined with concepts; see Section 3.3.
(Section 3.3 talks about concepts which is irrelevant now.)
sizeof... is just sugar, I think.
sizeof is indeed core to the language as is ..., and although a countof function could exist we already have sizeof and ... reserved so we might as well make it convenient to get the count that way.
Contrarily, if sizeof and ... weren't reserved, the idea of adding such a thing would have probably failed because new keywords tend to be frowned upon. (The less the better.)