0
votes

I have tried to compile this (--std=c++0x) using:

  • [FAIL with --std=c++0x flag] clang version 3.2-1~exp9ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2)
  • [FAIL without --std=c++0x flag] clang version 3.2-1~exp9ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2)
  • [FAIL without --std=c++11 flag] Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
  • [FAIL with --std=c++11 flag] Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
  • [PASS with --std=c++0x flag] gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1)
  • [FAIL without --std=c++0x flag] gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1)

When it fails at clang ubuntu, the following errors are produced:

test.cpp:26:37: error: no viable conversion from 'const P' to 'timeval'
        return static_cast<timeval>(_p);
                                    ^~
/usr/include/x86_64-linux-gnu/bits/time.h:30:8: note: candidate constructor
      (the implicit copy constructor) not viable: no known conversion from 'const P' to
      'const timeval &' for 1st argument
struct timeval
       ^
/usr/include/x86_64-linux-gnu/bits/time.h:30:8: note: candidate constructor
     (the implicit move constructor) not viable: no known conversion from 'const P' to 'timeval &&' for
      1st argument
struct timeval
       ^
test.cpp:9:5: note: candidate function
    operator const timeval() const {
    ^
test.cpp:13:5: note: candidate function
    operator const int8_t() const {
    ^

I am not sure what I am doing incorrectly.

#include <iostream>
#include <cstdint>

union P {
    timeval _timeval;
    int8_t  _int8_t;
    uint8_t _uint8_t;

    operator const timeval() const {
        return _timeval;
    }

    operator const int8_t() const {
        return _int8_t;
    }
};


struct Y {

    operator const int8_t() const {
        return static_cast<int8_t>(_p);
    }

    operator const timeval() const {
        return static_cast<timeval>(_p);
    }

    P _p;
};

int main()
{

    Y testobj;
    timeval ret = static_cast<timeval>(testobj);
    return 0;
}
2
You don't tell us what the error is, we close your question. - John Zwinck
@stackmate: I added errors in for you this time, but please remember to share what your compiler tells you: the error messages mean something, and they are important! - Bill
@bill thanks - the errors were very verbose - thought it would clog this thread and ontop of that I assumed I was doing something obviously wrong - stackmate

2 Answers

0
votes

You may need to remove the first const from the conversion operators, because you are trying to cast the union to a non-const value later:

operator timeval() const {
    return _timeval;
}

Abusing conversion operators seems like a bad idea. If you want encapsulation, consider creating proper methods like timeval getTimeval() const.

1
votes

As a starting point, you're missing the # from the beginnings of your #includes.

#include <iostream>
#include <cstdint>

You're also not including any header that should define the type timeval. Given the system(s) you're apparently using, you probably want:

#include <sys/time.h>

If you were using Windows, that would probably be:

#include <winsock2.h>

There are probably more problems, but that should at least get you started in the right general direction.