jeudi 10 novembre 2022

Get type contained in variant at run time in C++?

In C++, how do I print the type contained in a variant at run time?

My use case: passing a dictionary of values from Python to C++ using pybind11, and I want to print out the types that are received.

mercredi 9 novembre 2022

Nested class public method takes parent class as input

I found this in a 3rd party codebase

class Foo {
 public:
  class Log {
   public:
   static Log method(Foo& foo);
  };
};

I'm omitting many other methods from both classes for conciseness. It compiles, but it is giving me a hard time when trying to create python bindings around it via pybind11. Is this an acceptable practice or should I simply refactor the class to extract this Log class out of it?

How to use the function `pj_geodetic_to_geocentric` in boost library?

In boost/geometry/srs/projections/impl/pj_transform.hpp, Ifound a function pj_geodetic_to_geocentric, the function declaration is blow:

template <typename T, typename Range>
inline int pj_geocentric_to_geodetic( T const& a, T const& es,
                                      Range & range );
template <typename T, typename Range>
inline int pj_geodetic_to_geocentric( T const& a, T const& es,
                                      Range & range );

the function is manually converted from PROJ4, I am confused, how to use the function? the variable Range &range, how to pass a variable in the function?

I do not know how to use it!! help me please!

mardi 8 novembre 2022

How to randomize the name entered by the user

#include <iostream>
#include <algorithm>
#include <conio.h>
#include <vector>
#include <time.h>

using namespace std;

int main()
{
    int jumlah_orang;
    char user3;
    vector<string> names;
    string nama;
    srand(time(NULL));
    do
    {
        system("cls");
        cout << "Jumlah Orang: "; cin >> jumlah_orang;
        cin.ignore();
        for(int i = 0; i < jumlah_orang; i++)
        {
            cout << "Nama ke-" << i+1 << ": ";
            getline(cin, nama);
            names.push_back(nama);
        }
        cout << "\nNama:\n";
        for(int i = 0; i < names.size(); i++)
        {
            cout << names[i] << endl;
        }
        int random_name = rand() % names.size();
        cout << nama[random_name] << endl;
        cout << "\nUlang Program? (Y/T): "; 
        cin >> user3;
    }
    while(user3 == 'Y' || user3 == 'y');
    getch();
}

The name that comes out is always in the form of one character. For example, enter the name "Russel". The output can be "u", "s", "l", etc. This is not what I want.

I want users to enter the number of users and enter their respective names. When random is done, the output data is the name of one of the users entered.

how to return all read data to the input stream?

#include <iostream>
using namespace std;

int main ()
{
  int n, x;
  while(!cin.fail()) cin >> n;
  cout << n << ' ';
  //TODO
  cin >> n;
  cout << n;
  for(int i = 0; i < n - 1; i++){
    cin >> x;
    cout << x << ' ';
  }
}

This is my code. The task is to move all the elements to the right. That is, you must first display the last element, and then all the rest.

IMPORTANT!!! It needs to be done without using arrays.

In TODO. You need to write something so that the input stream returns to its original position.

Input example:

4 1 2 3 4

Output example:

4 1 2 3

I tried to read just the last element from the input stream, and then all the rest, but I didn’t succeed (((((

Is shared_ptr always free memory automatically?

You are partially replacing old code from the past. I'm replacing an existing pointer with a smart pointer for this operation, and I'm curious about deallocation. I replaced the code as below, will a leak occur?

std::vector<std::shared_ptr<Interface>> interface_list;

for (int i = 0 ; i < 5; ++i) {
  Interface *buffer = CreateFactory(i);
  if (buffer) interface_list.emplace_back(buffer);
}
...
for (std::shared_ptr<Interface> it: interface_list) {
  it.reset();
}
//IS iT ok?

Different results of std::isinf for different types and different compilers

Compiling with MSVC(v19.33), this does not compile (C2668 ambiguous call to overloaded function):

std::cout << std::isinf(0) << std::endl;

But this compiles:

std::cout << std::isinf(0.0) << std::endl;

However, in cppreference.com, it says:

  1. A set of overloads or a function template accepting the arg argument of any integral type. Equivalent to (2) (the argument is cast to double).

It seems like the function has existed in the standard since C++11.

If I understood correctly, the first code should be equivalent to the second one, shouldn't it? Does this mean MSVC hasn't implemented the overloaded functions yet, or am I missing something?

(In contrast, if I use gcc or clang, the above examples compile without problems).