newbie: simple question

M

Michal

hallo people.

how to write C++ style conversion in order for it to compile?

#include <sys/time.h>
#include <algorithm>
#include <iostream>

using namespace std;

typedef pair<unsigned long long, struct timeval> PerfDebData;

int main(int argc, char *argv[])
{
PerfDebData duration(0, static_cast<struct timeval>({0, 0}));
duration.first = 3;
cout << duration.first << endl;
return 0 ;
}


best regards,
Michal
 
V

Vladyslav Lazarenko

hallo people.

how to write C++ style conversion in order for it to compile?

#include <sys/time.h>
#include <algorithm>
#include <iostream>

using namespace std;

typedef pair<unsigned long long, struct timeval> PerfDebData;

int main(int argc, char *argv[])
{
  PerfDebData duration(0, static_cast<struct timeval>({0, 0}));
  duration.first = 3;
  cout << duration.first << endl;
    return 0 ;

}

best regards,
Michal

timeval is a C structure and does not have constructors. So you have
to initialize values of that structure yourself. But you cannot do it
like you tried. This is how you can do:

#include <sys/time.h>
#include <algorithm>
#include <iostream>

using namespace std;

typedef pair<unsigned long long, struct timeval> PerfDebData;

int main(int argc, char *argv[])
{
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
PerfDebData duration(0, tv);
duration.first = 3;
cout << duration.first << endl;
return 0 ;
}

For convenience, you may create a free function initializing timeval
structure:

#include <sys/time.h>
#include <algorithm>
#include <iostream>

using namespace std;

typedef pair<unsigned long long, struct timeval> PerfDebData;

template <typename TimeT, typename SecT>
static inline timeval make_timeval(TimeT sec, SecT usec) {
timeval res;
res.tv_sec = sec;
res.tv_usec = usec;
return res;
}

int main(int argc, char *argv[])
{
PerfDebData duration(0, make_timeval(1, 2));
duration.first = 3;
cout << duration.first << " -> (" << duration.second.tv_sec << ", "
<< duration.second.tv_usec << ")." << endl;
return 0 ;
}
 
R

red floyd

hallo people.
how to write C++ style conversion in order for it to compile?
#include <sys/time.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef pair<unsigned long long, struct timeval> PerfDebData;
int main(int argc, char *argv[])
{
  PerfDebData duration(0, static_cast<struct timeval>({0, 0}));
  duration.first = 3;
  cout << duration.first << endl;
    return 0 ;
}

timeval tv = { 0, 0 };
PerfDebData duration(0, tv);

Pete, wouldn't this work as well:

PerfDebData duration(0, timeval());

Since the () version of the constructor would zero-initialize all
members of timeval?
 
J

James Kanze

how to write C++ style conversion in order for it to compile?
#include <sys/time.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef pair<unsigned long long, struct timeval> PerfDebData;

Note that the "struct" isn't necessary.
int main(int argc, char *argv[])
{
PerfDebData duration(0, static_cast<struct timeval>({0, 0}));
duration.first = 3;
cout << duration.first << endl;
return 0 ;
}

If you want C++ style "conversions", you'll have to provide a
class with constructors. It's generally not worth it (just
provide a free function, as someone else suggested, and only
that if you need it more than once), you can derive from
timeval, and use the derived class everywhere in your C++. (Of
course, any C function which returns a timeval or a timeval*
will still continue to do so. So you're derived class should
have a constructor which takes one or both of these.)
 
M

Michal

Pete Becker said:
Since the () version of the constructor would zero-initialize all
members of timeval?

Sure, for all-zero initialization. Using an initializer lets you
provide other values as well.


Is it some part of standard, I mean, can You also do:
int a() to have a initialized with 0?
 
M

Michal

Not exactly, for reasons of poor syntax. Instead, use:
int a = int( );


so just to summarize:

int() and timeval() both create 0 initialized variables of their type,
while:

timeval a() and int b() do not guarantee it.

is it correct?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top