clause | A C metaprogramming library
kandi X-RAY | clause Summary
kandi X-RAY | clause Summary
A C++ metaprogramming library
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of clause
clause Key Features
clause Examples and Code Snippets
Community Discussions
Trending Discussions on clause
QUESTION
There are a number of different ways, that make a type/class usable in a ranged for loop. An overview is for example given on cppreference:
range-expression
is evaluated to determine the sequence or range to iterate. Each element of the sequence, in turn, is dereferenced and is used to initialize the variable with the type and name given in range-declaration.
begin_expr
andend_expr
are defined as follows:
- If
range-expression
is an expression of array type, thenbegin_expr
is__range
andend_expr
is (__range
+__bound
), where__bound
is the number of elements in the array (if the array has unknown size or is of an incomplete type, the program is ill-formed)- If
range-expression
is an expression of a class typeC
that has both a member namedbegin
and a member namedend
(regardless of the type or accessibility of such member), thenbegin_expr
is__range.begin()
andend_expr
is__range.end()
- Otherwise,
begin_expr
isbegin(__range)
andend_expr
isend(__range)
, which are found via argument-dependent lookup (non-ADL lookup is not performed).
If I want to use a ranged for loop say in a function template, I want to constrain the type to be usable in a ranged for loop, to trigger a compiler error with a nice "constraint not satisfied" message. Consider the following example:
...ANSWER
Answered 2022-Apr-14 at 09:51It seems like what you need is std::ranges::range
which requires the expressions ranges::begin(t)
and ranges::end(t)
to be well-formed.
Where ranges::begin
is defined in [range.access.begin]:
The name
ranges::begin
denotes a customization point object. Given a subexpressionE
with typeT
, let t be an lvalue that denotes the reified object forE
. Then:
If
E
is an rvalue andenable_borrowed_range>
isfalse
,ranges::begin(E)
is ill-formed.Otherwise, if
T
is an array type andremove_all_extents_t
is an incomplete type,ranges::begin(E)
is ill-formed with no diagnostic required.Otherwise, if T is an array type,
ranges::begin(E)
is expression-equivalent tot + 0
.Otherwise, if
auto(t.begin())
is a valid expression whose type modelsinput_or_output_iterator
,ranges::begin(E)
is expression-equivalent toauto(t.begin())
.Otherwise, if
T
is a class or enumeration type andauto(begin(t))
is a valid expression whose type modelsinput_or_output_iterator
with overload resolution performed in a context in which unqualified lookup forbegin
finds only the declarations
QUESTION
The following code compiles fine:
...ANSWER
Answered 2022-Mar-22 at 17:52Consider if we implement Fn
manually (of course this requires nightly)...
QUESTION
Update: the root issue was a bug which was fixed in Spark 3.2.0.
Input df structures are identic in both runs, but outputs are different. Only the second run returns desired result (df6
). I know I can use aliases for dataframes which would return desired result.
The question. What is the underlying Spark mechanics in creating df3
? Spark reads df1.c1 == df2.c2
in the join
's on
clause, but it's evident that it does not pay attention to the dfs provided. What's under the hood there? How to anticipate such behaviour?
First run (incorrect df3
result):
ANSWER
Answered 2021-Sep-24 at 16:19Spark for some reason doesn't distinguish your c1
and c2
columns correctly. This is the fix for df3
to have your expected result:
QUESTION
In Python, Python has Union type, which is convenient when a method can accept multi types:
...ANSWER
Answered 2022-Mar-08 at 23:25My answer (which is very similar to your first solution ;) would be:
QUESTION
(Note! This question particularly covers the state of C++14, before the introduction of inline variables in C++17)
TLDR; Question- What constitutes odr-use of a constexpr variable used in the definition of an inline function, such that multiple definitions of the function violates [basic.def.odr]/6?
(... likely [basic.def.odr]/3; but could this silently introduce UB in a program as soon as, say, the address of such a constexpr variable is taken in the context of the inline function's definition?)
TLDR example: does a program where doMath()
defined as follows:
ANSWER
Answered 2021-Sep-08 at 16:34In the OP's example with std::max
, an ODR violation does indeed occur, and the program is ill-formed NDR. To avoid this issue, you might consider one of the following fixes:
- give the
doMath
function internal linkage, or - move the declaration of
kTwo
insidedoMath
A variable that is used by an expression is considered to be odr-used unless there is a certain kind of simple proof that the reference to the variable can be replaced by the compile-time constant value of the variable without changing the result of the expression. If such a simple proof exists, then the standard requires the compiler perform such a replacement; consequently the variable is not odr-used (in particular, it does not require a definition, and the issue described by the OP would be avoided because none of the translation units in which doMath
is defined would actually reference a definition of kTwo
). If the expression is too complicated, however, then all bets are off. The compiler might still replace the variable with its value, in which case the program may work as you expect; or the program may exhibit bugs or crash. That's the reality with IFNDR programs.
The case where the variable is immediately passed by reference to a function, with the reference binding directly, is one common case where the variable is used in a way that is too complicated and the compiler is not required to determine whether or not it may be replaced by its compile-time constant value. This is because doing so would necessarily require inspecting the definition of the function (such as std::max
in this example).
You can "help" the compiler by writing int(kTwo)
and using that as the argument to std::max
as opposed to kTwo
itself; this prevents an odr-use since the lvalue-to-rvalue conversion is now immediately applied prior to calling the function. I don't think this is a great solution (I recommend one of the two solutions that I previously mentioned) but it has its uses (GoogleTest uses this in order to avoid introducing odr-uses in statements like EXPECT_EQ(2, kTwo)
).
If you want to know more about how to understand the precise definition of odr-use, involving "potential results of an expression e...", that would be best addressed with a separate question.
QUESTION
I have two enums:
Main Menu Options ...ANSWER
Answered 2022-Jan-03 at 19:57This is probably one of the cases where you need to pick one between being DRY and using enums.
Enums don't go very far as far as code reuse is concerned, in Java at least; and the main reason for this is that primary benefits of using enums are reaped in static code - I mean static as in "not dynamic"/"runtime", rather than static
:). Although you can "reduce" code duplication, you can hardly do much of that without introducing dependency (yes, that applies to adding a common API/interface, extracting the implementation of asListString
to a utility class). And that's still an undesirable trade-off.
Furthermore, if you must use an enum (for such reasons as built-in support for serialization, database mapping, JSON binding, or, well, because it's data enumeration, etc.), you have no choice but to duplicate method declarations to an extent, even if you can share the implementation: static methods just can't be inherited, and interface methods (of which getMessage
would be one) shall need an implementation everywhere. I mean this way of being "DRY" will have many ways of being inelegant.
If I were you, I would simply make this data completely dynamic
QUESTION
C 2018 6.7.6.1 1 says:
If, in the declaration “T D1”, D1 has the form
* type-qualifier-listopt D
and the type specified for ident in the declaration “T D” is “derived-declarator-type-list T”, then the type specified for ident is “derived-declarator-type-list type-qualifier-list pointer to T”. For each type qualifier in the list, ident is a so-qualified pointer.
This question is about the final sentence, but let’s work through the first one first.
Consider the declaration int * const * foo
. Here T is int
, D1 is * const * foo
, type-qualifier-list is const
, and D is * foo
.
Then T D is int * foo
, and that specifies “pointer to int
” for the ident foo
, so derived-declarator-type-list is “pointer to”. (There is no overt explanation of derived-declarator-type-list in the standard, but 6.7.8 3, discussing typedef
, says it “is specified by the declarators of D.”)
Substituting these into the final clause of the first sentence tells us that T D1 specifies the type for foo
is “pointer to const
pointer to int
”. Fine so far.
But then the final sentence tells us that for each type qualifier in the list (which is const
), ident is a so-qualified pointer. So it says foo
is a const
pointer.
But it is not; we commonly interpret int * const * foo
to declare foo
to be a non-const
pointer to a const
pointer to int
.
Is this a mistake in the standard or is there another interpretation for the final sentence?
...ANSWER
Answered 2021-Dec-04 at 08:18With T as int
and D as * foo
, the "T D" does not give ident the type T, but pointer to T.
The second part of the if
:
If ... and the type specified for ident in the declaration “T D” is “derived-declarator-type-list T”
seems to make sure that the nesting is correct.
With two stars, you would have to use D2 - D1 - D before you reach the direct declarator.
The second example:
QUESTION
There is a nice question (Which substitution failures are not allowed in requires clauses?) proposing the next problem.
One needs to write a compile-time function template constexpr bool allTypesUnique()
that will return true
if all argument types are unique, and false
otherwise. And the restriction is not to compare the argument types pairwise. Unfortunately, the answer only explains why such function cannot be implemented with some particular approach.
I think the solution can be achieved using multiple inheritance. The idea is to make a class inherited from a number of classes: one for each type T
in Ts
. And each such class defines a virtual function with a signature depending on T
. If some T
is found more than once in Ts
then function f
in a child class will override the function in a base class and it can be detected:
ANSWER
Answered 2021-Sep-18 at 21:35If you use virtual base classes depending on each of the given types, you will get exact one base class instance for every unique type in the resulting class. If the number of given types is the number of generated base classes, each type was unique. You can "measure" the number of generated base classes by its size but must take care that you have a vtable pointer inside which size is implementation dependent. As this, each generated type should be big enough to hide alignment problems.
BTW: It works also for reference types.
QUESTION
Consider this code:
...ANSWER
Answered 2021-Nov-01 at 21:47Yes, I think we can prove that a == 1 || b == 1
is always true. Most of the ideas here were worked out in comments by zwhconst and Peter Cordes, so I just thought I would write it up as an exercise.
(Note that X, Y, A, B below are used as the dummy variables in the standard's axioms, and may change from line to line. They do not coincide with the labels in your code.)
Suppose b = x.load()
in thread2 yields 0.
We do have the coherence ordering that you asked about. Specifically, if b = x.load
yields 0, then I claim that x.load()
in thread2 is coherence ordered before x.store(1)
in thread1, thanks to the third bullet in the definition of coherence ordering. For let A be x.load()
, B be x.store(1)
, and X be the initialization x{0}
(see below for quibble). Clearly X precedes B in the modification order of x
, since X happens-before B (synchronization occurs when the thread is started), and if b == 0
then A has read the value stored by X.
(There is possibly a gap here: initialization of an atomic object is not an atomic operation (3.18.1p3), so as worded, the coherence ordering does not apply to it. I have to believe it was intended to apply here, though. Anyway, we could dodge the issue by putting x.store(0, std::memory_order_relaxed);
in main
before starting the threads, which would still address the spirit of your question.)
Now in the definition of the ordering S, apply the second bullet with A = x.load()
and B = x.store(1)
as before, and Y being the atomic_thread_fence
in thread1. Then A is coherence-ordered before B, as we just showed; A is seq_cst
; and B happens-before Y by sequencing. So therefore A = x.load()
precedes Y = fence
in the order S.
Now suppose a = y.load()
in thread1 also yields 0.
By a similar argument to before, y.load()
is coherence ordered before y.store(1)
, and they are both seq_cst
, so y.load()
precedes y.store(1)
in S. Also, y.store(1)
precedes x.load()
in S by sequencing, and likewise atomic_thread_fence
precedes y.load()
in S. We therefore have in S:
x.load
precedesfence
precedesy.load
precedesy.store
precedesx.load
which is a cycle, contradicting the strict ordering of S.
QUESTION
In C++17, consider a case where S
is a struct with a deleted default constructor and a float member, when S is initialized with empty braces, is the float member guaranteed by the standard to be zero-initialized?
ANSWER
Answered 2021-Oct-18 at 20:55This is a quirk of C++ that is fixed in C++20. In the meantime you can add explicit
to the deleted default constructor to force the struct to become non-aggregate, and make your code a guaranteed compile error:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install clause
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page