vendredi 1 avril 2022

C++: Explicitly initialize base class

I'm working through a tutorial in C++ and am running into an Undefined symbols for architecture x86_64 error. The way my classes are structured looks like this:

class v_factory {
    // A pure abstract class
public:
    virtual ~v_factory() = 0;

    // a bunch of pure virtual functions

protected:
    // if i put this here, I don't get issue #1 showing up on my compiler
    v_factory() = default;
};

class use_factory : public v_factory {
public:
    // issue #1 will come up here if i don't have that protected default constructor for v_factory **ISSUE #1**
    explicit use_factory(Queue<char> &queue) : q(queue) {}

    // issue #2: not sure what to actually put here, left it as default for now.
    ~use_factory() override = default;

    // overriding a bunch of virtual functions from v_factory here.

private: 
    Queue<char> &q;
};

Issue #1: Constructor for 'use_factory' must explicitly initialize the base class 'v_factory' which does not have a default constructor Not sure how to go about this, if I add the default protected constructor, it goes away but I was looking through a book and I didn't see that a constructor was needed since it is a pure abstract class (since all functions are virtual). Maybe I misunderstood. Not sure what the proper fix would be for this.

Issue #2:

Undefined symbols for architecture x86_64:
  "v_factory::~v_factory()", referenced from:
      use_factory::~use_factory() in Main.cpp.o
ld: symbol(s) not found for architecture x86_64

I'm not sure how to approach this one either. The destructor on v_factory is virtual so doesn't it rely on use_factory to have one? How should I approach this?

EDIT for Issue #2: I believe this may have been resolved when reviewing a Geeksforgeeks page, essentially I believe if I define a destructor for both, I should be good to go.

Sorry for the lengthy question, any help would be greatly appreciated!

C++: Explicitly initialize base class AND undefined symbols for architecture x86_64 destructor

I'm working through a tutorial in C++ and am running into an Undefined symbols for architecture x86_64 error. The way my classes are structured looks like this:

class v_factory {
    // A pure abstract class
public:
    virtual ~v_factory() = 0;

    // a bunch of pure virtual functions

protected:
    // if i put this here, I don't get issue #1 showing up on my compiler
    v_factory() = default;
};

class use_factory : public v_factory {
public:
    // issue #1 will come up here if i don't have that protected default constructor for v_factory **ISSUE #1**
    explicit use_factory(Queue<char> &queue) : q(queue) {}

    // issue #2: not sure what to actually put here, left it as default for now.
    ~use_factory() override = default;

    // overriding a bunch of virtual functions from v_factory here.

private: 
    Queue<char> &q;
};

Issue #1: Constructor for 'use_factory' must explicitly initialize the base class 'v_factory' which does not have a default constructor Not sure how to go about this, if I add the default protected constructor, it goes away but I was looking through a book and I didn't see that a constructor was needed since it is a pure abstract class (since all functions are virtual). Maybe I misunderstood. Not sure what the proper fix would be for this.

Issue #2:

Undefined symbols for architecture x86_64:
  "v_factory::~v_factory()", referenced from:
      use_factory::~use_factory() in Main.cpp.o
ld: symbol(s) not found for architecture x86_64

I'm not sure how to approach this one either. The destructor on v_factory is virtual so doesn't it rely on use_factory to have one? How should I approach this?

Sorry for the lengthy question, any help would be greatly appreciated!

Standard C++ Parallelism using nvc++ is slow

I am not sure what i am doing wrong but it seems std::sort is much slower using nvc++ -stdpar than g++. Other std functions are better but never better than the multithreaded CPU version.

Below is the code where TIMEIT is just a macro to compute the before and after timer.

using Duration = std::chrono::duration<double, std::milli>;
std::random_device rd;
std::uniform_real_distribution<> dist(1,1000);

int main(){

int n=1<<21;
std::vector<float> v(n);

std::generate(v.begin(),v.end(),[&](){ return dist(rd);});
Duration d;
TIMEIT(d,
std::sort(std::execution::par,v.begin(),v.end());
std::cout<<d.count()<<"\n";

The results are

nvc++ -stdpar std.cu 217.164

g++ std.cpp -ltbb 69.9

Note that nvc++ issues a warning about a sequential run for cc less than 70 but when i try without -stdpar the time is 756 so i am guessing that it is parallelizing. If not, i am not sure how to force it to parallelize.

nvc++ 22.3 on WSL2 on Windows 11

GPU: GeForce GTX 1660 Ti with Max-Q Design, 6GB RAM

CPU: Intel I7-1065G7, 32 GB RAM

Spinlock code using atomic_flag in C++ is not compiling in Mac

I tried to write a simple Spinlock code in C++ but the code is not getting compiled in the Mac but is compiling on other gcc compilers.

#include<iostream>
#include<thread>
#include<atomic>
using namespace std;

class SpinLock 
{
    public:
        atomic_flag flag;
        SpinLock() : flag(ATOMIC_FLAG_INIT) {}
        void lock()
        {
            while(flag.test_and_set());
        }
        void unlock()
        {
            flag.clear();
        }
};

SpinLock spin;

void critical_section()
{
    spin.lock();
    for(int i = 0 ; i < 10 ; i++)
    {
        cout << i << " ";
    }
    cout << endl;
    spin.unlock();
}

int main()
{
    thread t1(critical_section);
    thread t2(critical_section);
    t1.join();
    t2.join();
}

I tried checking version of clang installed in my machine at the version is: Apple clang version 13.1.6 (clang-1316.0.21.2) Target: arm64-apple-darwin21.4.0 Thread model: posix

Can any one help me in the fact that why code is not getting compiled?

CRTP base private constructor and derived friend class cause compilation error using C++17 and uniform initialization

I've got the following code:

struct B
{
  B(int) {}
};

template <typename T>
class Base : public B
{
  friend T;
  Base() : B(1) {}
};

class Derived : public Base<Derived>
{
public:
    void do_sth() const {}
};

int main()
{
  auto x = Derived{}; //Compiles only when using C++11
  auto x1 = Derived(); //Compiles using C++17/20 flag
  x.do_sth();
  x1.do_sth();
} 

For some reason when using C++17 compilation fails due to 'non-compilable' initialization of 'x' variable. Compilator says that:

Base::Base() [with T = Derived]' is private within this context

but as you can see, below I'm creating an object of the same type but this time I'm not using uniform initialization. x1 variable can be compiled using both C++11 or C++17 standards, but the 'x' variable is compilable only in C++11 mode. Why is that? What has changed in the standard that causes this problem?

Compiler explorer

STL algorithm to convert std::vector

I have a vector of integers:

std::vector<int> values = {1,2,3,4,5,6,7,8,9,10};

I simply want to convert the adjacent elements into a pair, like this:

std::vector<std::pair<int,int>> values = { {1,2}, {3,4} , {5,6}, {7,8} ,{9,10} };

i.e.the two adjacent elements are joined into a pair.

What STL algorithm I can use to easily achieve this?

I don't know what STL algorithm to use. And I only want to achieve this through some standard algorithms.

What's the use of

The <ratio> header lets you uses template meta-programming to work with and manipulate rational values.

However - it was introduced in C++11, when we already had constexpr. Why is it not good enough to have a fully-constexpr'ifed library type for rationals over type I, and use that instead?

Is there some concrete benefit to using std::ratio that C++11 constexpr functionality would not be well-suited enough for? And if so, is it still relevant in C++20 (with the expanded "reach" of constexpr)?