no matching constructor for initialization of 'vlq' - c++

I'm getting no known conversion from 'vlq' to 'uint32_t' (compile log below) when compiling the following code:
// vlq.h
#ifndef __VARIABLE_LENGTH_QUANTITY_H__
#define __VARIABLE_LENGTH_QUANTITY_H__
#include <memory>
class vlq {
private:
std::unique_ptr<uint8_t[]> valueBytes; // pointer to value
size_t valueLength; // length of value
static size_t encodedLength(uint32_t a);
static uint8_t* encodeVlq(uint32_t a);
static uint32_t decodeVlq(uint8_t* valueBytes, size_t valueLength);
public:
vlq(); // defualt constructor
vlq(uint32_t value);
size_t length() const; // returns number of bytes to store vlq
uint32_t value() const; // returns encoded value of vlq
vlq operator+ (vlq const &obj);
};
#endif
// vlq.cpp
#include "vlq.h"
#include <stdexcept> // std::out_of_range
#include <stdint.h> // uint8_t, uint32_t
#include <cmath> // log2, floor
vlq::vlq() {
this->valueBytes.reset();
this->valueLength = 0;
}
vlq::vlq(uint32_t value) {
size_t valueLength = encodedLength(value);
uint8_t* valueBytes = encodeVlq(value);
this->valueBytes.reset(valueBytes);
this->valueLength = valueLength;
}
// returns required length for passed value
size_t vlq::encodedLength(uint32_t a) {
// calculate number of bits required
size_t numBits = std::floor(std::log2(a)) + 1;
// set length
size_t length;
if (numBits % 7 == 0) {
length = numBits / 7;
} else {
length = numBits / 7 + 1;
}
return length;
}
// encodes bytes of a vlq
uint8_t* vlq::encodeVlq(uint32_t a) {
size_t length = encodedLength(a);
uint8_t *formattedVlqBytes = new uint8_t[length];
// turn a into 7 bit chunks
int chunkOffset;
for (int i = 0; i < length; i++) {
chunkOffset = (length - i - 1) * 7;
formattedVlqBytes[i] = (a & (0x7F << chunkOffset)) >> chunkOffset;
// set bit if this isn't the last bit
if (i != length - 1) {
formattedVlqBytes[i] |= 0x80;
}
}
return formattedVlqBytes;
}
// decodes bytes of a vlq
uint32_t vlq::decodeVlq(uint8_t* valueBytes, size_t valueLength) {
size_t numBits = valueLength * 7;
uint32_t decodedVlq = 0x0;
size_t byteOffset, bitOffset;
for (size_t i = 0; i < numBits; i++) {
byteOffset = i / 7;
bitOffset = 6 - (i % 7);
decodedVlq |= valueBytes[byteOffset] & (0x1 << bitOffset) << (valueLength - byteOffset - 1) * 7;
}
return decodedVlq;
}
// returns encoded value of vlq
uint32_t vlq::value() const {
return decodeVlq(this->valueBytes.get(), this->valueLength);
}
vlq vlq::operator+(vlq const &obj) {
uint32_t sum = this->value() + obj.value();
return vlq(sum);
}
// compiler output
vlq.cpp:81:9: error: no matching constructor for initialization of 'vlq'
return vlq(sum);
^~~~~~~~
./vlq.h:6:7: note: candidate constructor (the implicit copy constructor) not viable: expects an l-value for 1st argument
class vlq {
^
vlq.cpp:11:6: note: candidate constructor not viable: no known conversion from 'vlq' to 'uint32_t' (aka 'unsigned int') for 1st argument
vlq::vlq(uint32_t value) {
^
vlq.cpp:6:6: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
vlq::vlq() {
^
1 error generated.
Why am I getting no known conversion from 'vlq' to 'uint32_t' when passing a uint32_t?

Related

invalid initialization of non-const reference of type 'MyVector<int>&' from an rvalue of type 'MyVector<int>'

I know that there are many threads on this, but after going through several, I still haven't solved my problem.
I have a custom vector class and I am trying to create a function that takes in a vector and adds it to the current vector, then returns it.
Below is the code for my vector class.
MyVector.h
template<class type>
class MyVector {
private:
type* m_data;
size_t m_capacity = 1;
size_t m_size = 0;
public:
MyVector() {
this->m_size = 0; // this->m_size is 0 because there are no elements
this->m_capacity = 1; // this->m_capacity starts off 1 more than this->size
this->m_data = new type[this->m_capacity]; // Allocate the memory required
}
MyVector(MyVector<type>& other) {
this->m_capacity = other.m_capacity; // The capacity will be the same
this->m_size = other.size(); // The size will also be the same
this->m_data = new type[this->m_capacity]; // Allocate the memory required to hold the data
/* Copy all the data from the vector to this object */
for (size_t i = 0; i < other.size(); i++) {
this->m_data[i] = other[i];
}
}
MyVector(std::initializer_list<type> init_list) {
int init_list_size = init_list.size(); // This will be the size of the initializer list: { 1, 2, 3, ... }
this->m_capacity = init_list_size + 1; // Make the capacity one more to make it work when trying to append
this->m_data = new type[this->m_capacity]; // Allocate the memory required
/* Copy all the data */
for (type t : init_list) {
this->m_data[this->m_size++] = t;
}
}
MyVector<type>& operator=(MyVector<type> other) {
this->m_capacity = other.m_capacity; // The capacity will be the same as the other vector
this->m_size = other.m_size; // The size will be the same as the other vector
this->m_data = new type[this->m_capacity]; // Allocate the memory required
/* Copy the data from the other vector to this */
for (size_t i = 0; i < other.size(); i++) {
this->append(other.get(i));
}
return *this;
}
/* Functionality to append, delete, etc... */
type get(size_t index) const {
return this->m_data[index];
}
/* PROBLEMATIC CODE */
MyVector<type> add(MyVector<type>& other) {
MyVector<type> result;
for (size_t i = 0; i < this->size(); i++) {
result.append( this->get(i) + other.get(i) );
}
return result;
}
};
The following is how I implemented the MyVector class.
main.cpp
#include "../include/MyVector.h"
int main() {
MyVector<int> a = { 1, 2, 3, 4, 5 };
MyVector<int> b = a;
MyVector<int> c = a.add(b); // error: invalid initialization of non-const reference of type 'MyVector<int>&' from an rvalue of type 'MyVector<int>'
return 0;
}
The error I get is error: invalid initialization of non-const reference of type 'MyVector&' from an rvalue of type 'MyVector'.
Along with note: initializing argument 1 of 'MyVector::MyVector(MyVector&) [with type = int]'
What I don't understand is why the left-side of the equation cannot be assigned to an rvalue.

atomicCAS for bool implementation

I'm trying to figure out is there a bug in the answer (now deleted) about the implementation of Cuda-like atomicCAS for bools. The code from the answer (reformatted):
static __inline__ __device__ bool atomicCAS(bool *address, bool compare, bool val)
{
unsigned long long addr = (unsigned long long)address;
unsigned pos = addr & 7; // byte position within the unsigned long long
int *int_addr = (int *)(addr - pos); // int-aligned address
int old = *int_addr, assumed, ival;
do
{
assumed = old;
if(val)
ival = old | (1 << (8 * pos));
else
ival = old & (~((0xFFU) << (8 * pos)));
old = atomicCAS(int_addr, assumed, ival);
} while(assumed != old);
return (bool)(old & ((0xFFU) << (8 * pos)));
}
According to the documentation, atomicCAS should set *address to (*address == compare ? val : *address), but in the implementation above compare argument is never used!
The code I use to reproduce the bug:
#include <cstdio>
// atomicCAS definition here
__device__ bool b;
__global__ void kernel()
{
b = false;
atomicCAS(&b, true, true); // `(b == true ? true : b)`, where b is false equals to false
printf("%d\n", b); // b is false => expected output is 0
}
int main()
{
kernel<<<1, 1>>>();
cudaDeviceSynchronize();
}
The expected output is 0, but the actual output is 1.
I have a suggestion about how to fix it but am not 100% sure it's thread-safe:
static __inline__ __device__ bool atomicCAS(bool *address, bool compare, bool val)
{
unsigned long long addr = (unsigned long long)address;
unsigned pos = addr & 3; // byte position within the int
int *int_addr = (int *)(addr - pos); // int-aligned address
int old = *int_addr, assumed, ival;
do
{
if(*address != compare) // If we expected that bool to be different, then
break; // stop trying to update it and just return it's current value
assumed = old;
if(val)
ival = old | (1 << (8 * pos));
else
ival = old & (~((0xFFU) << (8 * pos)));
old = atomicCAS(int_addr, assumed, ival);
} while(assumed != old);
return (bool)(old & ((0xFFU) << (8 * pos)));
}
My questions are
Is there a bug in the first code sample from the answer? If there is,
Does the last code sample fix it thread-safely?
Many many thanks to #RobertCrovella; the first code sample does contain a bug, the second does fix it, but is not thread-safe (see question comments for details). The thread-safe fix:
static __inline__ __device__ bool atomicCAS(bool *address, bool compare, bool val)
{
unsigned long long addr = (unsigned long long)address;
unsigned pos = addr & 3; // byte position within the int
int *int_addr = (int *)(addr - pos); // int-aligned address
int old = *int_addr, assumed, ival;
bool current_value;
do
{
current_value = (bool)(old & ((0xFFU) << (8 * pos)));
if(current_value != compare) // If we expected that bool to be different, then
break; // stop trying to update it and just return it's current value
assumed = old;
if(val)
ival = old | (1 << (8 * pos));
else
ival = old & (~((0xFFU) << (8 * pos)));
old = atomicCAS(int_addr, assumed, ival);
} while(assumed != old);
return current_value;
}

Compilation error appears because of missing const?

I am trying to run a code that defines objects that are a collection of English letters.
I dont know why it does not compile.
I have tried to change from int to const int but it is not the case,
and also added the disable 4996 message but it didnt help.
#include <iostream>
using namespace std;
class CharSet
{
int size;
char* pSet;
public:
// -----------------------------------
CharSet(int const size, char* set)
{
this->size = size;
pSet = new char[strlen(set) + 1];
strcpy(pSet, set);
}
// -----------------------------------
~CharSet()
{
delete[] pSet;
}
// -----------------------------------
CharSet operator*(const CharSet & other)
{
int maxSize = 0;
if (this->size >= other.size)
maxSize = this->size;
else
maxSize = other.size;
char * ret = new char[maxSize + 1];
char temp;
int index = 0;
for (int i = 0; i < this->size; i++)
{
temp = this->pSet[i];
for (int j = 0; j < other.size; j++)
{
if (other.pSet[j] == temp)
{
ret[index] = temp;
index++;
}
}
}
ret[index] = '\0';
return CharSet(maxSize, ret);
}
// -----------------------------------
bool operator()(char check)
{
bool flag = false;
for (int i = 0; i < this->size; i++)
{
if (pSet[i] == check)
flag = true;
}
return flag;
}
// -----------------------------------
friend ostream& operator<<(ostream& os, const CharSet& s)
{
os << s.pSet;
return os;
}
// -----------------------------------
};
int main()
{
CharSet s1(4, "DAQP"), s2(3, "AXZ");
cout << s1 * s2 << endl;
if (s1('Q') == true)
cout << "The element is member of the set" << endl;
else
cout << "The element is not member of the set" << endl;
return 0;
}
errors:
E0289 no instance of constructor "CharSet::CharSet" matches the argument
E0289 no instance of constructor "CharSet::CharSet" matches the argument list
C4996 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
C2664 'CharSet::CharSet(const CharSet &)': cannot convert argument 2 from
C2664 'CharSet::CharSet(const CharSet &)': cannot convert argument 2 from 'const char [4]' to 'char *'
you need a const char* in your constructor:
CharSet(int const size, const char* set)
Thanks to
#holy black cat
"DAQP" is a const char[] which you didn't provide a constructor for that(the array will implicitly convert to pointer).
A better way is using std::string:
class CharSet
{
std::string pSet;
public:
// -----------------------------------
CharSet(std::string set) : pSet(set)
{
}
// -----------------------------------
~CharSet()
{
}
// -----------------------------------
CharSet operator*(const CharSet & other)
{
int maxSize = 0;
std::string ret;
char temp;
int index = 0;
for (int i = 0; i < pSet.size(); i++)
{
temp = pSet[i];
for (int j = 0; j < other.pSet.size(); j++)
{
if (other.pSet[j] == temp)
{
ret += temp;
index++;
}
}
}
return CharSet(ret);
}
// the rest of members ...
//
};
full code at godblot

Modifying bits in a byte with a class

I want to directly modify a bit in a byte.
In GCC, you can do it as follow:
struct virtualByte {
unsigned char b0 : 1;
unsigned char b1 : 1;
unsigned char b2 : 1;
unsigned char b3 : 1;
unsigned char b4 : 1;
unsigned char b5 : 1;
unsigned char b6 : 1;
unsigned char b7 : 1;
} __attribute__((__packed__));
#define sbit(_byte, _pos) (((volatile struct virtualByte *)&_byte)->b ## _pos)
Usage:
unsigned char myByte = 0x00;
#define firstBit sbit(myByte, 0)
firstBit = 1; // Implicit myByte |= 0x01;
To make things neater I want to have class that does this for me. I came up with the following concept:
unsigned char myByteRef = 0x00;
Byte myByte(&myByteRef);
myByte[0] = 1; // Implicit myByteRef |= 0x01;
fprintf(stderr, "%2.2X\n", myByteRef);
But this does not work because in c++ you cannot return a reference to a single bit. Overloading the constructor does not work either.
Is there a possibility to implement such behaviour? The assignment operator should directly modify its underlying byte (not a set of bytes).
You want to use std::bitset:
std::bitset<12> someBits; // 12 bits
someBits[0] = true; // set 1st bit
std::cout << someBits.count() << '\n'; // prints 1
std::bitset<12>::reference bit5 = someBits[5];
bit5 = true;
std::cout << someBits.count() << '\n'; // prints 2
You can use the index operator to return a reference to a bit in the way you want. Note that this reference is not a bool& but rather a std::bitset::reference:
Finally came to a solution, many thanks to #doc!
My solution:
class Bit {
private:
volatile uint8_t *byte;
uint8_t bitPos;
public:
Bit(void)
{
}
void init(volatile uint8_t *const byte, uint8_t const bitPos)
{
this->byte = byte;
this->bitPos = (bitPos > 7u ? 7u : bitPos);
}
void setValue(bool const bitValue)
{
if (!this->byte) return;
if (bitValue) {
*this->byte |= (1u << this->bitPos);
} else {
*this->byte &= ~(1u << this->bitPos);
}
}
};
class BitReference {
private:
Bit &ref;
public:
BitReference(Bit &ref) : ref(ref)
{
}
void operator=(bool const bitValue)
{
this->ref.setValue(bitValue);
}
};
class Byte {
private:
Bit bits[8];
public:
Byte(volatile uint8_t *const byte)
{
for (unsigned i = 0; i < 8; ++i) {
this->bits[i].init(byte, i);
}
}
/* This did the trick :)! */
BitReference operator[](size_t index)
{
if (index > 7) index = 7;
return BitReference(this->bits[index]);
}
};
Usage:
uint8_t myPort = 0x00;
int main(int const argc, const char **const argv)
{
Byte abc(&myPort);
abc[0] = 1;
abc[1] = 1;
abc[2] = 1;
abc[3] = 1;
fprintf(stderr, "%2.2X\n", myPort);
return 0;
}

Optimization of loops and if

I have a procedure looks like this:
void Process1(unsigned char* data)
{
}
void Process2(unsigned char* data)
{
}
void Process3(unsigned char* data)
{
}
#define FLAG1 (1 << 1)
#define FLAG2 (1 << 2)
#define FLAG3 (1 << 3)
void ProcessData(unsigned char* data, unsigned int bytes, unsigned int flags)
{
bool b1 = !!(flags & FLAG1);
bool b2 = !!(flags & FLAG2);
bool b3 = !!(flags & FLAG3);
for (unsigned int i = 0; i < bytes; i ++)
{
if (b1) Process1(data + i);
if (b2) Process2(data + i);
if (b3) Process3(data + i);
}
}
As it looks, flags & FLAG1 A.K.A b1 will not change in all the loops. But we still have to do branch in every loop. I just wondering if there's a way to avoid this unnecessary branch dynamically.
here is a demo of Lundin's solution.
#include <windows.h>
#include <stdio.h>
#include <time.h>
LARGE_INTEGER ls, le, ll;
#define START_CLOCK() QueryPerformanceCounter(&ls)
#define END_CLOCK() printf ("%.0lf ns\n", (QueryPerformanceCounter(&le), ((double)le.QuadPart - ls.QuadPart) / ll.QuadPart * 1000000));
void Process1(unsigned char* data)
{
(*data)++;
}
void Process2(unsigned char* data)
{
(*data)--;
}
void Process3(unsigned char* data)
{
(*data) *= (*data);
}
#define FLAG1 (1 << 1)
#define FLAG2 (1 << 2)
#define FLAG3 (1 << 3)
void ProcessData(unsigned char* data, unsigned int bytes, unsigned int flags)
{
bool b1 = !!(flags & FLAG1);
bool b2 = !!(flags & FLAG2);
bool b3 = !!(flags & FLAG3);
for (unsigned int i = 0; i < bytes; i ++)
{
if (b1) Process1(data + i);
if (b2) Process2(data + i);
if (b3) Process3(data + i);
}
}
typedef void (*proc_t)(unsigned char*);
inline static void do_nothing (unsigned char* ptr)
{
(void)ptr;
}
void ProcessData_x(unsigned char* data, unsigned int bytes, unsigned int flags)
{
bool b1 = (flags & FLAG1) != 0; // de-obfuscate the boolean logic
bool b2 = (flags & FLAG2) != 0;
bool b3 = (flags & FLAG3) != 0;
proc_t p1 = b1 ? Process1 : do_nothing;
proc_t p2 = b2 ? Process2 : do_nothing;
proc_t p3 = b3 ? Process3 : do_nothing;
for (unsigned int i = 0; i<bytes; i++)
{
p1(data + i);
p2(data + i);
p3(data + i);
}
}
int main()
{
if (!QueryPerformanceFrequency(&ll)) return 1;
const unsigned int bytes = 0xffff;
srand((unsigned int)time(NULL));
unsigned int flags = rand() & 0x7;
unsigned char* data = new unsigned char[bytes];
for (unsigned int i = 0; i < bytes; i++)
{
data[i] = (unsigned char)(rand() & 0xff);
}
START_CLOCK();
ProcessData(data, bytes, flags);
END_CLOCK();
START_CLOCK();
ProcessData_x(data, bytes, flags);
END_CLOCK();
}
here is the output:
134 ns
272 ns
I've run it several times but, unexpectedly, it costs even more time:(.. it is also compiled 'vs2010 Release x86'
First of all, it doesn't any sense to speak about optimizations without a particular system in mind...
That being said, I'd optimize away the branches in the following way:
typedef void (*proc_t)(unsigned char*);
inline static void do_nothing (unsigned char* ptr)
{
(void)ptr;
}
...
void ProcessData(unsigned char* data, unsigned int bytes, unsigned int flags)
{
bool b1 = (flags & FLAG1) != 0; // de-obfuscate the boolean logic
bool b2 = (flags & FLAG2) != 0;
bool b3 = (flags & FLAG3) != 0;
proc_t p1 = b1 ? Process1 : do_nothing;
proc_t p2 = b2 ? Process2 : do_nothing;
proc_t p3 = b3 ? Process3 : do_nothing;
for (unsigned int i = 0; i<bytes; i++)
{
p1(data + i);
p2(data + i);
p3(data + i);
}
}
A c++ solution. Similar to Lundin's answer but without calls to empty function. I'm not sure if that makes any difference in performance, the main advantage is that you don't need to manually list all the process calls in the loop. If you want to micro optimize or want c, you could use an array on stack, but you'll have to manage some counters yourself.
typedef void (*proc_t)(unsigned char*);
std::vector<proc_t> processes;
if (b1) processes.push_back(Process1);
if (b2) processes.push_back(Process2);
if (b3) processes.push_back(Process3);
for(auto p : processes)
for (unsigned int i = 0; i<bytes; i++)
p(data + i);
bool b1 = !!(flags & FLAG1);
bool b2 = !!(flags & FLAG2);
bool b3 = !!(flags & FLAG3);
int caseNow=SelectCaseAtOnce(b1,b2,b3);
if(caseNow==0)
for (unsigned int i = 0; i < bytes; i ++)
{
Process1(data + i);
}
else if(caseNow==1)
for (unsigned int i = 0; i < bytes; i ++)
{
Process2(data + i);
}
else if(caseNow==2)
for (unsigned int i = 0; i < bytes; i ++)
{
Process3(data + i);
}
else if(caseNow==3)
for (unsigned int i = 0; i < bytes; i ++)
{
Process1(data + i);
Process2(data + i);
}
if(caseNow==4)
for (unsigned int i = 0; i < bytes; i ++)
{
Process1(data + i);
Process3(data + i);
}
else if(caseNow==5)
for (unsigned int i = 0; i < bytes; i ++)
{
Process2(data + i);
Process3(data + i);
}
else if(caseNow==6)
for (unsigned int i = 0; i < bytes; i ++)
{
Process1(data + i);
Process2(data + i);
Process3(data + i);
}
else {}
Here's another solution using templates - this way you'll get an optimized version of the inner loop for each variant. If the ProcessN functions are short / simple enough to be worth inlining then this could be a worthwhile optimization.
#include <tuple>
#include <map>
#include <utility>
using namespace std;
inline void Process1(unsigned char* data) {}
inline void Process2(unsigned char* data) {}
inline void Process3(unsigned char* data) {}
#define FLAG1 (1 << 1)
#define FLAG2 (1 << 2)
#define FLAG3 (1 << 3)
template <bool b1, bool b2, bool b3>
void ProcessData(unsigned char* data, unsigned int bytes) {
for (unsigned int i = 0; i < bytes; i++) {
if (b1) Process1(data + i);
if (b2) Process2(data + i);
if (b3) Process3(data + i);
}
}
void ProcessData(unsigned char* data, unsigned int bytes, unsigned int flags) {
typedef void (*ProcessFunc)(unsigned char*, unsigned int bytes);
static map<tuple<bool, bool, bool>, ProcessFunc> funcs{
{make_tuple(false, false, false), ProcessData<false, false, false>},
{make_tuple(false, false, true), ProcessData<false, false, true>},
{make_tuple(false, true, false), ProcessData<false, true, false>},
{make_tuple(false, true, true), ProcessData<false, true, true>},
{make_tuple(true, false, false), ProcessData<true, false, false>},
{make_tuple(true, false, true), ProcessData<true, false, true>},
{make_tuple(true, true, false), ProcessData<true, true, false>},
{make_tuple(true, true, true), ProcessData<true, true, true>}};
bool b1 = !!(flags & FLAG1);
bool b2 = !!(flags & FLAG2);
bool b3 = !!(flags & FLAG3);
funcs[make_tuple(b1, b2, b3)](data, bytes);
}