language-lawyer

Linkage of function declared as `extern` in block scope according to the C++17 standard draft

时光毁灭记忆、已成空白 提交于 2020-05-29 06:08:10
问题 From the C++17 Standard Draft § 3.5.6 : The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage. If there is a visible declaration of an entity with linkage having the same name and type , ignoring entities declared outside the innermost enclosing namespace scope, the block scope declaration declares that same entity and receives the linkage of the previous declaration. If there is more than one such matching entity,

Can I add a deduction guide to `std` namespace?

十年热恋 提交于 2020-05-29 03:45:26
问题 Suppose I want to make a new deduction guide making the following possible ? std::string str; std::basic_string_view sv = str; Would that be an Ok customization ? 回答1: [namespace.std]/2.4: The behavior of a C++ program is undefined if it declares [...] a deduction guide for any standard library class template. 来源: https://stackoverflow.com/questions/58992794/can-i-add-a-deduction-guide-to-std-namespace

Is every “normal” use of user-defined literals undefined behavior?

萝らか妹 提交于 2020-05-28 14:05:54
问题 User defined literals must start with an underscore. This is a more or less universally well-known rule that you can find on every layman-worded site talking about user literals. It is also a rule which I (and possibly others?) have been blatantly ignoring ever since on a "what a bullshit" base. Now of course, that's strictly not correct. In the strictest sense, this uses a reserved identifier, and thus invokes Undefined Behavior (although you don't get as much as a shrug from the compiler,

Reference Class Member Triviality

一曲冷凌霜 提交于 2020-05-28 04:02:31
问题 Let's say, for reasons of my own, I want a class to have a non-static reference member. I would think that this type should be easily optimized out of most code that uses it. Therefore, I assert its triviality in a unit test. Clang and GCC agree that the class is trivial, but MSVC disagrees. Who is right, and why, according to the standard? #include <type_traits> struct example { int& r; }; // Clang and GCC let this pass // MSVC fires this assertion static_assert( std::is_trivial<example>:

C++ name lookup - example from the standard

时光毁灭记忆、已成空白 提交于 2020-05-27 18:18:42
问题 I need an explanation on this example from standard [basic.lookup.unqual]/3: typedef int f; namespace N { struct A { friend void f(A &); operator int(); void g(A a) { int i = f(a); // f is the typedef, not the friend // function: equivalent to int(a) } }; } I would argue that void N::f(A &) is closer than int(a) because it does not involve the type-cast operator. I cannot however be sure because the standard contains only one instance of "type cast". By the way, compiling that code fails in

Handing over locked std::unique_lock to new threads

谁都会走 提交于 2020-05-27 07:26:10
问题 Consider the following example where I create a std::mutex , lock it and then hand the lock over to another thread : #include <future> #include <mutex> int main() { // Create and lock a mutex std::mutex mutex; std::unique_lock<decltype(mutex)> lock(mutex); // Hand off the lock to another thread auto promise = std::async(std::launch::async, [lock{ std::move(lock) }]() mutable { // Unlock the mutex lock.unlock(); }); promise.get(); return 0; } The example seems to run fine with gcc 6.3 but

Handing over locked std::unique_lock to new threads

丶灬走出姿态 提交于 2020-05-27 07:24:40
问题 Consider the following example where I create a std::mutex , lock it and then hand the lock over to another thread : #include <future> #include <mutex> int main() { // Create and lock a mutex std::mutex mutex; std::unique_lock<decltype(mutex)> lock(mutex); // Hand off the lock to another thread auto promise = std::async(std::launch::async, [lock{ std::move(lock) }]() mutable { // Unlock the mutex lock.unlock(); }); promise.get(); return 0; } The example seems to run fine with gcc 6.3 but

Deduction of trailing template-argument in declaration of explicit specializations of function templates (no function arg. deduction)

对着背影说爱祢 提交于 2020-05-26 05:15:32
问题 (This question is a branch-out from the discussion in the comments of Template specialization of variable template and type deduction.) [temp.expl.spec]/10 states that [ emphasis mine]: A trailing template-argument can be left unspecified in the template-id naming an explicit function template specialization provided it can be deduced from the function argument type . [ Example: template<class T> class Array { /* ... */ }; template<class T> void sort(Array<T>& v); // explicit specialization

How a stream error indicator affects following input code?

三世轮回 提交于 2020-05-23 07:00:27
问题 Each stream has "an error indicator that records whether a read/write error has occurred". It is set, usually rarely, by various functions: fgetc(), fflush(), fseek(), ... . It is cleared by various functions: rewind(), clearerr(), fopen(), ... . int ferror(FILE *stream) reports the state. The ferror function returns nonzero if and only if the error indicator is set for stream . In this case, certainly an input error just occurred. if (!ferror(istream)) { int ch = fgetc(istream); if (ch ==

Constant padding in Verilog

末鹿安然 提交于 2020-05-16 01:56:05
问题 Here is the example behavioral Verilog code in question module constant; reg [7:0] foo; initial begin foo = 1'bz; $display("%H", foo); end endmodule Icarus Verilog gave me $ iverilog -o constant constant.v $ ./constant 0Z However, according to this website (and the lecturer of an FPGA course I am taking), If number is smaller than the size constant, then it will be padded to the left with zeros. If the most significant bit of a specified number has an unknown (x) or high-impedance (z) value,