Is timespec not defined in Windows? - windows

It seems weird to me that this answer is hard to find. I've included time.h and ctime, but vc9 is still complaining about an undefined type 'timespec'. I've searched here, MSDN, and the web (even with the exact compiler error), but I can't find the answer... maybe it's just lost in the noise.
Here's the exact error:
error C2027: use of undefined type 'timespec'
Thanks

struct timespec comes from posix, and are typically found on unixes, but not on windows.

If you are trying to compile code with a *nix-y provenance under Windows then you might be better off with something like cygwin and gcc, which gives you a *nix-like environment.

Try including pthread.h . That is where timespec is defined in my ming32 compiler that comes packaged with Codeblocks.
Why? I dont know. Just...
#include <time.h>
#ifndef POSIX MORALITY
#include <pthread.h>
#endif
Solution 2...
You dont need a prefined timespec! You can define it yourself! Which is what mingw does!
struct timespec { /* details */ };
You dont even have to create timespec. You can create struct Fun or struct Jav::timespec; with the same structure and cast it to timespec*. Doing this makes your code more portable when indeed timespec is defined.

Related

std::hash no type named "hash_policy" when running ska::flat_hash_map

I'm primarily an R programmer, and I'm using Rcpp to run a hash map implementation by Malte Skarupke called ska::flat_hash_map on Windows 10 through RStudio (Microsoft OpenR). The C++ compiler is g++ run with c11 flags.
With no changes to his .hpp file, I am unable to get it running, as it produces the error
Line 276 no type named 'hash_policy' in 'struct std::hash<char>'
The offending line in flat_hash_map.hpp is
template<typename T>
struct HashPolicySelector<T, void_t<typename T::hash_policy>>
{
typedef typename T::hash_policy type;
};
I've found a few benchmark libraries on github that seem to include the library with no problems, and access it like std::unordered_map, so I don't understand why I am having problems getting it to run.
I've also tried providing different types instead of char, sticking to the ones that std::hash should be able to handle automatically, such as int, and std::string.
My source file is really simple, as I'm literally just trying to get a hash map created, for example, my last run was using this:
#include <Rcpp.h>
#include "flat_hash_map.hpp"
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
void run_test()
{
ska::flat_hash_map<char,char> test_map;
}
I'm hoping someone with more C++ experience than myself could shed some light on the problem, or try running the library themselves if the issue is reproducible.
Thanks for the help!
This is also my first post on StackOverflow, please let me know if there is something I can do to improve my question.
GCC < 5.0 would not trigger substitution failure on unused parameters within an alias template. This case was actually underspecified in the standard, eventually solved by CWG Issue 1558.
As a workaround, you should manually replace line 266:
template<typename...> using void_t = void;
with:
template <typename...>
struct voider { using type = void; };
template <typename... Ts>
using void_t = typename voider<Ts...>::type;
This forces the usage of template parameters of the alias template, allowing the compiler to SFINAE-out types that don't declare hash_policy.

Error: function template already inherited - with icpc

I am writing a class that inherits from std::vector (*) and I want to be able to construct it in all the ways that are available for its base, so I use
#include <vector>
struct B : public std::vector<double>
{
using std::vector<double>::vector;
};
This compiles fine with the g++ and clang, but the Intel compiler gives me (with flag -std=c++11)
error: function template "B::B(_ForwardIterator, _ForwardIterator, const std::__1::allocator<double> &)" already inherited from "std::__1::vector<double, std::__1::allocator<double>>"
at the using directive.
Any ideas why this is happening and/or how to solve it?
Update: it would seem this is OS dependent, works fine on Linux and Windows platforms but fails on OS X. A discussion is ongoing on the Intel forums.
(*) I don't want to use polymorphism, please avoid "this is bad practice" answers if not directly relevant - there are in fact numerous posts that agree this is acceptable.

What to do if tgamma() function is not defined?

I am trying to use tgamma() from the standard library. When I try to compile, I get the error message:
Call to undefined function tgamma
I have the directive #include <cmath>. I use Embarcadero C++ Builder XE3, which claims to support C++11 standards. What could be my problem, and how to fix it?
Boost contains a tgamma function.
#include <boost/math/special_functions/gamma.hpp>
...
double rootPi = boost::math::tgamma<double>(0.5);
Of course, you can always switch to a different compiler, like gcc.

Why "Redefinition of typedef" error with GCC 4.3 but not GCC 4.6?

I've been developing my application using GCC 4.6 for compilation but a user reported the "redefinition of typedef" error. After some confusion where I wrongfully told the user to fetch the latest source using Git, I took a closer look and found something similar to this:
/* mystruct.h */
#ifdef MYSTRUCT_H
#define MYSTRUCT_H
typedef struct _mystruct mystruct
#endif
/* mystruct.c */
#include "mystruct.h"
typedef struct _mystruct
{
int a;
int b;
} mystruct;
Which is quite easy to fix for the older GCC versions, but why is this no longer an error? Or does it need reporting?
This looks like a deliberate change...
I don't have 4.6 to hand, but it looks like you will get the warning if you use -pedantic.

Boost.asio compilation problem: undefined reference to `__sync_add_and_fetch_8

Hey guys,
This could be a noob question, but I really can't find any useful solution through Google.
I'm testing a hello world with boost.asio, the program is quite simple:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
std::cout << "Hello, world!\n";
return 0;
}
I passed the compilation and run well on my Intel Pentium PC (Ubuntu 10.10, gcc 4.4.5, Boost 1.46.0). The command line I used was
g++ -o a a.cpp -I /Boost-Include-Path/ -L /Boost-lib-Path/ -lboost_system
But when I compile the same code on another machine(which is a big one, I'll explain it later), it can't pass the compilation and gives such errors:
/tmp/ccOZxZBX.o: In function boost::asio::detail::gcc_sync_fenced_block::gcc_sync_fenced_block()': a.cpp:(.text._ZN5boost4asio6detail21gcc_sync_fenced_blockC1Ev[boost::asio::detail::gcc_sync_fenced_block::gcc_sync_fenced_block()]+0x4c): undefined reference to__sync_lock_test_and_set_4' /tmp/ccOZxZBX.o: In function boost::detail::atomic_count::operator++()': a.cpp:(.text._ZN5boost6detail12atomic_countppEv[boost::detail::atomic_count::operator++()]+0x30): undefined reference to__sync_add_and_fetch_8' /tmp/ccOZxZBX.o: In function boost::detail::atomic_count::operator--()': a.cpp:(.text._ZN5boost6detail12atomic_countmmEv[boost::detail::atomic_count::operator--()]+0x30): undefined reference to__sync_add_and_fetch_8' /tmp/ccOZxZBX.o: In function boost::detail::atomic_count::operator long() const': a.cpp:(.text._ZNK5boost6detail12atomic_countcvlEv[boost::detail::atomic_count::operator long() const]+0x30): undefined reference to__sync_fetch_and_add_8'
The machine I used was a SiCortex SC5832,which use MIPS64 instruction set processors, OS is changed CentoOS. Gcc 4.2.3, Boost1.46.0. Is it possible that there are problems about the compatibility of the MIPS? I added -mips64 option, but it still give the same errors.
I know this environment could not be very usual, but I think some people who are using similar big machines may face the same problem.
Any help would be appreciated. By the way, I don't have sudo permission.
Thanks,
Tony
This function is a GCC built-in function, and it was introduced around GCC 4.2 (iirc) see documentation.
According to the documentation it's not available on all target processors.
If you look at boost/smart_ptr/detail/atomic_count.hpp it looks like it would fall into the #elif defined(BOOST_SP_HAS_SYNC) block. i.e. boost/smart_ptr/detail/atomic_count_sync.hpp.
Support for this is determined in boost/smart_ptr/detail/sp_has_sync.hpp. This header essentially assumes that GCC supports this on all platforms except for a few exceptions. You might want to insert MIPS as another exception here and submit a patch to boost.
You'll also see that one workaround is to define BOOST_AC_USE_PTHREADS. This will use a mutex around the atomic count, which probably is significantly less efficient, but at least it will work until you can figure out what atomic operations are supported on MIPS64.

Resources