Forum Stats

  • 3,827,534 Users
  • 2,260,790 Discussions
  • 7,897,295 Comments

Discussions

Solaris Studio C++ compiler not performing automatic conversion on array arguments

koval
koval Member Posts: 72 Blue Ribbon

I try to compile clang libraries with Solaris Studio C++ compiler.

Errors occur when processing APInt class.

I have isolated the error:

template<typename T>
struct array_ref {
    template<unsigned N>
    array_ref(const T(&)[N]) {}
};

struct ap_int {
    ap_int(array_ref<int>) {}
};

int main()
{
    int array[2] = { 0, 1 };
    ap_int k(array);
    return 0;
}

This leads to

line 14: Error: Cannot use int[2] to initialize ap_int

I removed const qualifier from array_ref constructor:

template<typename T>
struct array_ref {
    template<unsigned N>
    array_ref(T(&)[N]) {}
};

struct ap_int {
    ap_int(array_ref<int>) {}
};

int main()
{
    int array[2] = { 0, 1 };
    ap_int k(array);
    return 0;
}

Now the error changes:

line 14: Error: Formal argument 1 of type array_ref<int> int call to ap_int::ap_int(array_ref<int>) is being passed int*

Let's go further. In the first version (with const) the code below (name it INIT for reference) produces error

array_ref<int> aref(array);

Error: Cannot use int[2] to initialize array_ref<int>

Without const-qualifier in constructor the above compiles fine.

There is a way to accept a const array as an argument but that requires SFINAE trick:

template<typename E, typename A>
struct array_t {};

template<typename E, unsigned N>
struct array_t<E, E[N]> {
    typedef E* type;
};

template<typename T>
struct array_ref {
    template<typename Arr>
    array_ref(const Arr&, typename array_t<T, Arr>::type = 0) {}
};

Now INIT compiles without errors but automatic conversion (initializing ap_int with array) still does not work:

Error: Formal argument 1 of type array_ref<int> int call to ap_int::ap_int(array_ref<int>) is being passed int*

The only way is to add a constructor with array in ap_int class but this is only a workaround - there is obviously a bug in the compiler.

Tested with:

  • Sun Studio 10 (CC: Sun C++ 5.7 2005/01/07)
  • Sun Studio 11 (CC: Sun C++ 5.8 Patch 121017-10 2007/02/21)
  • Sun Studio 12 (CC: Sun C++ 5.9 SunOS_sparc Patch 123863-01 2007/07/25)
  • Solaris Studio 12.3 (CC: Sun C++ 5.12 SunOS_sparc 2011/11/16).

All versions give same errors

I wonder how the next release (12.4) will handle this case

Tagged:
koval

Comments

  • Hi,

    You can download the 12.4 beta from  here:

    http://www.oracle.com/technetwork/server-storage/solarisstudio/downloads/solaris-studio-beta-2112763.html

    The code snippet compiles with the 12.4 beta.

    Regards,


    Darryl.

    koval
  • Fedor-Oracle
    Fedor-Oracle Member Posts: 131

    We substantially improved our standard compliance for the template code in 12.4.

    Previous versions of Studio C++ were indeed somewhat lacking in that area...

    regards,

    __Fedor.

  • koval
    koval Member Posts: 72 Blue Ribbon

    Thanks Darryl,

    I tried to find this download through Oracle site but only found a site where 12.4 beta was only available for premium members.

    Regards,

    Piotr

  • I hope the earlier link enabled you to try out the beta. Let us know if you have any feedback.

    Regards,


    Darryl.

  • koval
    koval Member Posts: 72 Blue Ribbon

    I'll give it a try, it is very good news that Solaris C++ supports C++11 at last.

    Regarding the earlier versions and Fedor's opinion:

    I faced some bugs in template area but most of the time there is an easy workaround. In case of automatic conversion from array arguments lots of manual work is required (adding array overloads for all methods which use array_ref).

    Still, I find CC quite a decent compiler wrt. templates, requiring less workarounds than e.g. earlier VC++.

    As for the other areas of standard, this is probably the only compiler which puts <cmath>, <cstring> and other <cXXX> headers in std namespace only, without polluting the global namespace.

    Also, CC's optimizer is very good, producing faster code than gcc in many cases.

  • Great! Thanks for the comments.


    D.

  • Fedor-Oracle
    Fedor-Oracle Member Posts: 131
    edited Apr 21, 2014 4:13PM

    > Still, I find CC quite a decent compiler wrt. templates, requiring less workarounds than e.g. earlier VC++.

    Well, not that much of an achievement... too many compilers around are better with templates than earlier VC++

    > the only compiler which puts <cmath>, <cstring> and other <cXXX> headers in std namespace only

    Yeah, and that gives us some troubles

    Say, Boost has quite a number of bugs when they use standard functions like strcpy from a global namespace.

    And as Studio is pretty much the only compiler who fails to compile the code because of this,

    people naturally start blaming our compiler...

    Well, Boost tends to fix these bugs as we report them, but then they introduce new ones of the same nature.

    regards,

    __Fedor.

  • koval
    koval Member Posts: 72 Blue Ribbon

    I can confirm that automatic conversion from template arguments, both const and non-const works on 12.4, even in c++03 mode.

    The only problem I will face in my project is that -std=c++11 cannot be used together with -library=Cstd. We are using a 3-rd party library (binary only) which is only available for libCstd which means we will not be able to use the new standard

    I can understand though, that it is not possible to handle two standards with one ABI which is why a different standard library has to be used with C++11.

    Another interesting thing:

    CC does not allow classes (even POD-like) with assignment operator in unions. This is absolutely correct and standard-compliant in C++03 mode but g++ somehow seems to allow classes with simple types only to be members of unions regardless of the existance of assignment operators.

    This "feature" is used in Qt 5 (json library) and LLVM (a number of unions containing types from Endian.h) and breaks (correcly!) compilation under CC. Thus, Boost is not the only one example of taking g++ as a standard instead of reading the language specification

This discussion has been closed.