This question already has answers here:
Portable way to check if expression compiles
(2 answers)
How to check at compile time that an expression is illegal?
(2 answers)
Closed 3 days ago.
Following this question where I ask how to test if an expression that tries to use an input iterator for write compiles (disclaimer, it shouldn't!), I would like to know if it's possible to have a generic solution that, given an expression, we are able to check if the expression will compile or not.
Take an easy example, the input iterator in the linked post. If I try to write with an input iterator:
collections::Array arr = collections::Array<int, 5>{1, 2, 3, 4, 5};
auto it_begin = arr.begin();
*it_begin = 7;
that shouldn't compile, because the operator*() returns a const&.
So, what would be the way, of, given an expression, test if it will compiles?
Extra points (well, just take this in consideration if it's possible):
No macros involved
No SFINAE related stuff
Related
This question already has answers here:
Reference referring to multiple objects, how is it possible? [duplicate]
(2 answers)
Why reference const can be re-assigned in for-statement?
(3 answers)
Closed 21 days ago.
for (int& i : array) {
//do something on i
}
Does this count as "reseating reference i to different objects"?
This, appearently, let i refers to array[0] in first iteration, array[1] in second iteration, ..., and so forth.
Edit 1 in response to comment: I know exact what happens in implementation level. I used compiler explorer to see what does this compile to and I know that this looks completely same as other type of iteration (some level of optimization is assumed). This question is more about on the language level, not implementation.
This question already has answers here:
Do structured bindings and forwarding references mix well?
(1 answer)
What are use cases for structured bindings?
(4 answers)
Closed 1 year ago.
I've seen some people talk about the new abstraction of C++17 in for ranged loops, where one can use multiple iterators/values/references:
for(auto&& [output1, output2] : container)
{...}
I've been trying to find an explanation on Google for it, unsuccessfully, since I believe I'm not using the right terminology. In any case I'd be happy if somebody had a link to the cppreference, as well as answer these questions:
1. Are we using "&&" because [output1, output2] as a whole is an rvalue?
2. Let's assume that output1 and output2 are both of type int. What is the type of "auto"? In other words, what kind of (rvalue?) object is [output1, output2], in case I wanted to not use "auto" and declare it specifically? (Maybe a tuple?)
3. What is happening under the hood? Are two iterators being used behind the scenes (see below)? What would be the non C++17 implementation using real iterator objects?
for(Container::iterator it1 =..., Container::iteratorit2 = ...; it1!=..., it2!=...; ++it1, ++it2)
{...}
This question already has answers here:
Why use non-member begin and end functions in C++11?
(7 answers)
Closed 4 years ago.
In this question (https://stackoverflow.com/questions/6926433/how-to-shuffle-a-stdvector) user703016 used the following syntax for accessing the iterators of vector cards_:
For C++98 they recommended using: cards_.begin() and cards_.end()
For C++11 they recommended using: std::begin(cards_) and std::end(cards_)
For C++14, which syntax is preferable, and is there any real difference between the two? Prior to today I've only seen the first syntax.
As long as you're dealing with a vector, it makes no difference which you use.
std::begin() and std::end() will, however, work in some situations where .begin() and .end() won't work. The primary one is working with a built-in array. For example, this will work fine:
char foo[] = "17395";
std::sort(std::begin(foo), std::end(foo));
Of course foo.begin() and foo.end() can't possibly work, since a built-in array doesn't have any member functions.
This question already has answers here:
Can I assume (bool)true == (int)1 for any C++ compiler?
(5 answers)
Closed 7 years ago.
Is it correct to compare bool to 1?
In a legacy code I find often:
if (xyz.isCounterActive() == 1)
where sCounterActive() returns bool.
Obviously, if ( xyz.isCounterActive() ) is sufficient, but If I change this, I don't know which side-effects it may cause. Software is big, buggy but the customer insists, that it is working.
Compiler is VS2008
In this case result of xyz.isCounterActive() will be implicitly converted to int. There're many rules of implicit conversion, which can be found here, for example.
Probably signature of isCounterActive changed since it was introduced, and the one, who changed it, forgot to modify all isCounterActive calls.
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 8 years ago.
Today, I see such a wierd way of indexing the arry.
The code is like:
int array[] = {10, 20, 30};
cout << -2[array];
I've never seen such a strange way of using array. But there is no compilation error.
Can anyone tell me does the ISO document evolve the description for this way of using array?
It works, because expressions of the form x[y] is just sugar for *(x+y), and of course, the addition is commutative, so 2[array] and array[2] get compiled to the same thing.
Don't do it though, because it's unnecessarily confusing.