mercredi 1 janvier 2020

Why can "Could not convert from '

With C++11, the following code snippet fails to compile due to error: could not convert '{((__Direct*)this)->__Direct::data}' from '<brace-enclosed initializer list>' to 'Direct':

struct Direct {
  uint8_t data[10][2];
};
struct __Direct {
  uint8_t data[10][2];

  Direct decompress() {
    return {data};
  }

  static __Direct compress(Direct in) {
    return {in.data};
  }
};

But I can work around this error by wrapping the multi-dimensional C array in a struct:

struct Wrapper {
  uint8_t data[10][2];
};
struct Wrapped {
  Wrapper tbl;
};
struct __Wrapped {
  Wrapper tbl;

  Wrapped decompress() {
    return {tbl};
  }

  static __Wrapped compress(Wrapped in) {
    return {in.tbl};
  }
};

Logically it seems like these could compile the same. I suspect C++ does not do bound checking when it encounters the explicit passing of a multi-dimensional C array (and is attempting to cast as uint8_t*[], causing the brace initializer to not match).

What C++ specification(s) describe this behavior?

c++ 'defined in both module' error for global functions (only happens in 64 bit mode) [closed]

I'm using Embarcadeo c++ Builder 10.2. When I compile in 32 bit mode (debug or release) I get no errors. In 64 bit mode I get 372 'defined in both module' errors each relating to a global inline function I've declared in globals.h.

From the thread C++: Multiple definition error for global functions in the header file I find that preceding the declaration with inline removes the errors but was wondering if anyone can cast any light on why it only happens with 64 bit compiles. I'm using the built in clang compiler for both 32 bit & 64 bit compiles.

minimal example

file Dummy.h

#ifndef DummyH
#define DummyH
//---------------------------------------------------------------------------
void x() {};
#endif

added

#include "Dummy.h"

to globals.h

In 32 bit mode I get no error but in 64 bit mode I get "symbol x() defined in both module …..". Redeclaring x() as

inline void x() {}; 

removes the error in 64 bit mode.

Finding content-based algorithm with it's time complexity and space complexity [closed]

I am working on my website which is basically for students discount and I want to use the recommendation system on my website so I choose content-based algorithm now I searching from but I didn't find any proper content-based recommendation algorithm with it time and space complexity.

std::result_of syntax kerfuffle

I have the following (CUDA) function:

__device__ auto foo(my_class& x, my_function_ptr_type y ) {
    return [gen](my_class& x) { return os.set_y(y); };
}

And I want typedef its return value's type. I've fiddling with the std::result_of syntax, and can't get it quite right. This won't work:

using foo_return_type = std::result_of<decltype(foo(my_class{}, my_function_ptr_type{nullptr}))>::type;

Nor this:

using foo_return_type = std::result_of<decltype(foo(my_class, my_function_ptr_type))>::type;

Nor this:

using foo_return_type = std::result_of<foo>::type;

What should I have as the template-argument to std::result_of?

Notes:

  • There's only one foo() in the namespace.
  • No templates are involved (other than std::result_of...)
  • C++11 or C++14, take your pick (but note that this is CUDA, so theoretically that could be an issue).
  • Compiler: NVCC 10.1

How to check if fixed width integers are defined

In C++, fixed width integers are defined as optional, but I can't seems to find the recommended way to check if they are actually defined.

What would be a portable way to check if fixed width integers are available?

What is the emplace_back behaviour? [duplicate]

This question already has an answer here:

Given this

    vector<pair<int,int>> v;
    int k=0;
    v.emplace_back(k, ++k);
    v.emplace_back(k, k++);
    cout << v[0].first << "," << v[0].second << endl;
    cout << v[1].first << "," << v[1].second << endl;

Output is

1,1
2,1

I'd have expected to behave like a function call:

0,1
1,1

Anyone can explain what's going on?

mardi 31 décembre 2019

Cannot change member variable via std::set iterator

I have two friend classes:

class Node {
private:
    unsigned id_;
    bool marked_;
    std::set<Node> neighbors_;

    friend class Graph;
    ......

public:
bool operator == (const Node & other) const { return id_ == other.id(); }
bool operator < (const Node & other) const { return id_ < other.id(); }
......
};

class Graph {
private:
    std::set<Node> vertices_{};
    void reach_set_helper(unsigned id, std::vector<unsigned> &reach_set);
    ......
};

I am trying to create a function that can first find a specific node in the graph vertices_, say node v. And then I want to change the marked_ property of the neighbors of v. To find v, I have to use std::find() method. However, the return iterator of this method does not allow me to change member variables of neighbors. This is what I have tried:

Node & s = v;
std::set<Node>::iterator pos = vertices_.find(s);
const std::set<Node> & neighbors = (pos->neighbors_);
for (std::set<Node>::iterator it = neighbors.begin(); it != neighbors.end(); it++) {
    if (!it->is_marked()) {
        reach_set.push_back(it->id());
        it->set_marked(true);
    }
    this->reach_set_helper(it->id(), reach_set);
}

Notice here that I have to use const std::set<Node> & neighbors because I want to change neighbors in place. But I can't change neighbors of v through const iterator it, so this method does not work. I have a way to make changes to vertices_ by erase and copy back through iterator, that's not a problem. But here it is different, I am doing operations on elements of vertices, which is another set. Any suggestions?