Compiling with operator() under GCC 3.3 Problem in Boost - c++

I have the following snippet:
#include <boost/random/lognormal_distribution.hpp>
#include <boost/random/lagged_fibonacci.hpp>
int main() {
const double mean = 0.0;
const double sigma = 1.0;
boost::lognormal_distribution<double> lognorm_dist(mean, sigma);
boost::lagged_fibonacci44497 engine;
// the following line give error in GCC 3.3
const double value = lognorm_dist.operator() <boost::lagged_fibonacci44497>((engine));
}
It compile fine under
i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465)
But under:
g++ (GCC) 3.3.3 (SuSE Linux)
It gave the following error:
Mycode.cc:10:error: `operator()' not defined
How can I fix the problem?

Why not just lognorm_dist( engine );? Providing "function-like" syntax is the whole point of operator(). That said, lognorm_dist.template operator() <boost::lagged_fibonacci44497>((engine)) should solve your compilation issues if I am not mistaken.

Related

Why does the following program using boost lexical cast fail on the Intel Compiler but not gcc (compiler bug)?

I have the following very simple program that seems to fail on the Intel compiler but not gcc when using boost::lexical_cast (boost version 1.67.0):
#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>
float getSomeNumber()
{
return 10.0;
}
int main(int argc, char** argv)
{
std::string mystring("Hello: " + boost::lexical_cast<std::string>(getSomeNumber());
std::cout << mystring << std::endl;
return 0;
}
When compiling, Intel returns the following error:
/boost/1.67.0/include/boost/type_traits/is_complete.hpp(45): warning #70: incomplete type is not allowed
ok_tag<sizeof(T)> check_is_complete(int);
detected during ....
...
/boost/1.67.0/include/boost/type_traits/is_complete.hpp(51): error: template instantiation resulted in unexpected function type of "boost::detail::ok_tag<1U> (int)" (the meaning of a name may have changed since the template declaration -- the type of the template is "boost::detail::ok_tag<sizeof(T)> (int)")
...
detected during:
instantiation of "boost::detail::check_is_complete" based on template argument <void> at line 51
instantiation of class "boost::is_complete<T> [with T=void]" at line 484 of "/Path/to/boost/include/boost/type_traits/is_convertible.hpp"
...
instantiation of "Target boost::lexical_cast<Target, Source>(const Source &) [with Target=std::string, Source=float]" at line 11 of "boostTest.cc"
The best I can tell from this error is it seems like the compiler can't complete the template resolution, but then the very end it seems like it does? I tried compiling the same thing under gcc (version 4.8.5) and it worked fine. This code also previously worked fine under boost 1.64.0 with both gcc and Intel.
Is this a bug in the Intel Compiler, or perhaps a bug in boost? Is there a way to modify my program as a workaround?
Software versions
gcc: 4.8.5
Intel Compiler: 2015.3.187
Boost: 1.67.0
OS: RHEL 7.5 (Maipo)
Compile line:
icpc/gcc --std=c++11 boostTest.cc -o boostTest -Ipath/to/boost/include -lstdc++

Header `execution` and `std::reduce` not found

I am trying to get this snippet to compile
#include <vector>
#include <numeric>
#include <execution>
double result = std::reduce(std::execution::par, v.begin(), v.end());
I tried these compilers:
Apple LLVM version 8.1.0 (clang-802.0.42)
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
All three give me 'execution' file not found
respectively error: no member named 'reduce' in namespace 'std'
auto result = std::reduce(v.begin(), v.end());
for this snippet
#include<numeric>
#include<vector>
int main(int argc, char *argv[])
{
std::vector<double> v(10, 1);
auto result = std::reduce(v.begin(), v.end());
return 0;
}
I guess my compilers are too old? But on cppreference it does not say which compiler version is requiered minimum and also I do not see any newer versions for clang or gcc in the repo.
std::reduce and std::execution::par are available since C++17.
For most of the compilers C++17 isn't fully implemented yet. You can try using clang with flag -std=c++1z.
I upgraded my GCC to version 10 and it compiled fine the std::execution::par with its include <execution> (which was failing to locate with gcc version 7.5.0). I followed to the instructions in this link: https://tuxamito.com/wiki/index.php/Installing_newer_GCC_versions_in_Ubuntu

C++ Ternary operator with scope resolution and condition

Following code is not being compiled by an specific compiler.
#include <iostream>
using namespace std;
class A{
public:
static const int x = 12;
static const int y = 16;
};
int main(){
int a = 12, b = 19;
int z = (a==b)?(A::x):(A::y);
cout<<z<<endl;
return 0;
}
Compiler g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11) compiled it successfully.
Compiler g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17) is causing compilation error
test.cpp:(.text+0x20): undefined reference to `A::x'
test.cpp:(.text+0x28): undefined reference to `A::y'
If I replace condition (a==b) in line int z = (a==b)?(A::x):(A::y); by true or false, then it gets compiled successfully.
What is the reason and how to fix it for specified compiler?
Looks like a weak/buggy C++0x symbol-linkage implementation of gcc 4.4.
Seems that gcc 4.4 tells the linker that there are symbols but it forgot to "implement" them in one of the compilation units.
I guess if you put the initialization of the static members A::x and A::y explicitly into one unique compilation unit (e.g. corresponding .cpp file) then your code may be compatible with both compilers.

CUDA 6.5 with g++ does not support c++11?

I read it here that CUDA 6.5 has started support for C++11 :
https://groups.google.com/forum/#!topic/thrust-users/R37GIkMG4tk
But when I compile an example code below, I got
$ nvcc -std=c++11 cu-gcc11.cu -o test
nvcc warning : The -c++11 flag is not supported with the configured host compiler. Flag will be ignored.
cu-gcc11.cu(7): error: explicit type is missing ("int" assumed)
My setting : CUDA 6.5, g++ 4.5, ubuntu 12.04
Codes :
#include <cuda.h>
#include <iostream>
__host__ void test() {
float a = 12.;
double b = 3.;
auto c = a * b;
std::cout << c << std::endl;
}
int main()
{
test();
return 0;
}
C++11 support in nvcc is experimental at this time. In order to properly use it you will need an appropriate configuration. This is not documented anywhere AFAIK, but you should have good results with either Fedora 20 or Ubuntu 14.04, both of which are supported configs for cuda 6.5 and include GCC 4.8.x.
In your case your GCC version is just too old.
I don't think -std=c++11 was available in GCC 4.5. Try -std=c++0x.

std::function<> and the Intel compiler version 11.1

I'm having trouble working with lambda functions in the Intel compiler, in particular, the following code won't compile:
template<typename T>
std::function<T (int)> make_func(T x) {
return [=](int index) -> T
{
return x;
};
}
The error I get is
error: namespace "std" has no member "function"
The code compiles and runs fine on my Mac, (macports gcc version 4.5). The error is at work, where we use the Intel compiler version 11.1. It does accept lambda functions (with the -std=c++0x option), such as:
auto lam = [=](int j) -> int {
printf("testing for lambdas: %d\t%d\n", n, j);
return n;
};
int g = lam(7);
The version of gcc installed at work is 4.1.2, so I'm guessing that the standard library is old?
/bin/libc.so.6
says it's version 2.5 compiled with gcc 4.1.2.
Is there a way around this?
thanks in advance for any help
I get the same behavior with icc 11.1 on a system where gcc 4.5.2 is installed.
g++'s header <functional> is protected with #ifdef __GXX_EXPERIMENTAL_CXX0X__ which is not defined when icc is used.
I would consider switching to boost::function in this setup, which of course works with icc.
Well, the code shown doesn't include a single header. And yet you refer to the standard library std::function.
So no, it doesn't compile. As with any other part of the standard library, you need to include the header where std::function is defined: <functional>.