Question regarding error

This doesn’t compile. Should it?:

class test
{
public:
test( float x );
test(attribute((vector_size(16))) float v );
};
int vi = 0;
void func()
{
test object = test(vi);
}

C:\Sony\Clang\exp>clang -cc1 scalar.cpp
scalar.cpp:13:16: error: functional-style cast from ‘int’ to ‘test’ is not allowed
test object = test(vi);
^~~~

The logic for computing implicit conversion sequences in C++ does not know anything about vectors. What you're seeing here is a poor diagnostic due to the cast. Change it to a direct initialization and you'll see the ambiguity:

t2.cpp:10:7: error: call to constructor of 'test' is ambiguous
test object(vi);
      ^ ~~
t2.cpp:4:2: note: candidate constructor
test( float x );
^
t2.cpp:5:2: note: candidate constructor
test(__attribute__((vector_size(16))) float v );
^
t2.cpp:1:7: note: candidate is the implicit copy constructor
class test
      ^

Clang can't figure out which constructor is the better match.

  - Doug

Right, it's treating it like a class which has constructors for, for
example, float and double. It does this because our routines for
identifying types do strange things for queries like
isRealFloatingType(). See also
http://llvm.org/bugs/show_bug.cgi?id=4208 .

-Eli

Note that in the actual code, the vector constructor is explicit:

class test
{
public:
test( float x );
explicit test(attribute((vector_size(16))) float v );
};

Shouldn’t that help the compiler decide that it’s not a candidate? (gcc doesn’t seem to care either way.)

I have one class like this that is used frequently. I added a constructor taking an int, but that caused other problems. Adding a (float) cast to the argument in the instanciations is the hack that seems to work.

Also, because you can’t cast scalars to vectors, I think the implicit conversion code should exclude the vector case. If you agree, I’ll file a bug.

Note that in the actual code, the vector constructor is explicit:

class test
{
public:
test( float x );
explicit test(__attribute__((vector_size(16))) float v );
};
Shouldn't that help the compiler decide that it's not a candidate? (gcc
doesn't seem to care either way.)

I have one class like this that is used frequently. I added a constructor
taking an int, but that caused other problems. Adding a (float) cast to the
argument in the instanciations is the hack that seems to work.

Also, because you can't cast scalars to vectors, I think the implicit
conversion code should exclude the vector case. If you agree, I'll file a
bug.

Yes, please file.

-Eli