On 9/30/2015 10:37 AM, Paul wrote:
> In the code below, why isn't {3,4} recognized immediately as a vector<int>?
> Why does it fail to compile if I replace
>
> if(vec == std::vector<int>({3,4}))
>
> by
>
> if(vec == {3,4}) ?
>
> void printVectorIfSpecial(const std::vector<int>& vec)
> {
> if(vec == std::vector<int>({3,4}))
> {
> // do something
> }
>
> }
I'm not sure. Possibly due to pure grammar issues. Consider that the
following works as intended:
<code>
#include <assert.h>
#include <vector>
#include <stdio.h>
void printVectorIfSpecial(const std::vector<int>& vec)
{
if( operator==( vec, {3,4} ) )
{
assert( vec.size() == 2 ); // Doesn't trigger: size is 2.
printf( "{3,4}.size() = %d.\n", int( vec.size() ) );
}
}
auto main() -> int
{
printVectorIfSpecial( std::vector<int>{3,4} );
}
</code>
The assert is just about the possibility that without knowing exactly
what it does, {3,4} /could/ be invoking the 2-argument vector
constructor that would create a vector of 4 items, each of value 3.
But given that the code works, it's hard (for me) to say why your
original isn't permitted.
Cheers, & sorry if that doesn't really help, but,
- Alf