Problem with boost::intrusive_ptr - boost

There is a struct which contain intrusive_ptr field:
struct BranchFeedback : boost::counted_base {
...
boost::intrusive_ptr<BPredState> theBPState;
};
There is another varibale which is defined as
std::vector< std::vector< BPredState > > theFetchState;
Now I have instantiated an object
BranchFeedback theFeedback;
and want to assign theFetchState to that field
theFeedback.theBPState = theFetchState[anIndex][!anOne];
However compiler says some errors
error: no match for ‘operator=’ in theFeedback.theBPState = .....
How can I fix that?

you're passing in a BPredState, but intrusive_ptr only supports operator= for pointers to the contained type (or other intrusive_ptrs)
so you could write theBPState = &(theFetchState[anIndex][!anOne]); or get an pointer or iterator to the element and use that instead.

Related

C++ no instance of constructor matches the argument list argument types are: (std::string, double)

Hello i'm learning from a littlebit bad book and when i finished the chapter i didnt find anything about this error and i tried aprox one week to fix it but i didnt afford it so i came here xd. Here some code the "declaration":
class variable {
public:
string name;
double value;
};
And the:
double define_name(string var, double val)
{
if (is_declared(var)) error(var, "declared twice");
var_table.push_back(variable(var,val));
return val;
}
This is the code where i got the following error in visual studio underlining the "variable "
(E0289) no instance of constructor "varaible::variable" matches the argument list
The error message given to you by the compiler is precise and correct. There are two possible fixes here:
define the constructor
use brace initialization
More details:
Defining a constructor:
class variable {
public:
variable(string n = {}, double v = 0.0) : name{n}, value{v} {}
string name;
double value;
};
Specifying reasonable default values ensures you can use the same constructor for "default" instantiations as well.
As for the second method: all you need to do is use the brace initialization, like the following:
var_table.push_back( {var, val} );
The way this works is it matches (by position) the provided initialization list to the members of the struct/class, using defaults (0 for numbers, default-constructors for user-defined types) for the omitted trailing ones, i.e. var_table.push_back( {var} ); is equivalent to var_table.push_back( {var, {}} ); which is equivalent to var_table.push_back( {var, 0.0} );

Template template in c++11 error

I've been reading the chapter 5 of "C++ Templates, The complete guide", and i've seen the concept of "template template" templates, so tried myself.
In a template class, i've declared my template like this:
template <typename TipoClave, typename TipoDato,
template <class Elem, class Alloc = allocator<Elem>> class Lista = vector>
And this works. My problem comes when I try to use a different container than the default.
My class is as follows:
class Tabla
{
public:
struct Celda {
TipoClave clave;
TipoDato dato;
};
Tabla(unsigned tam)
{
t.resize(tam);
}
///< Some other functions
private:
typedef Lista<Celda> ListaDatos;
Lista<ListaDatos> t;
};
And then, when I try to use it from the main program like this:
int main (void)
{
Tabla<string,Alumno,array> tabla(200);
///< Some stuff
return 0;
}
But this line Tabla<string,Alumno,array> tabla(200); does not compile, getting me an error like:
test_tabla_t.cpp: In function ‘int main()’: test_tabla_t.cpp:20:27:
error: type/value mismatch at argument 3 in template parameter list
for ‘template class Lista> class Tabla’ Tabla
tabla(200);
I've tried to use Tabla<string,Alumno,vector> tabla(200);, and it works, so I don't know how to solve this error.
Assuming you're trying to use std::array, it does not take an Allocator template parameter. Its second argument is the size of the array.
template<
class T,
std::size_t N
> struct array;
You haven't mentioned how experienced you are with C++, but since you're trying to learn more, I'll say that I've been using C++ for nearly a decade and can count on one hand the number of times I've used template template parameters.
In this case, you want Lista to be "something that acts like a sequence container[1]", you might like to read about concepts[2], which may make it into C++20. They allow you to tell the compiler the interface you want a template parameter to have.
In the meantime, it might be easier to declare Lista as a plain template parameter.
[1] http://en.cppreference.com/w/cpp/concept/SequenceContainer
[2] https://en.wikipedia.org/wiki/Concepts_(C%2B%2B)

Remove CV type qualifiers from libclang's CXType

I use libclang to parse source file and get reference to some type as CXType, say it is "const std::__1::basic_string<char>" (as reported by clang_getTypeSpelling). How can I get reference to same type but without const qualifier?
I was able to do this by visiting the type's cursor's children. For example, given a CXCursor 'cursor',
CXType type = clang_getCursorType(cursor);
if (clang_isConstQualifiedType(type))
{
auto RemoveConstFromType = [](CXCursor c, CXCursor, CXClientData d)
{
*(CXType*)d = clang_getCursorType(c);
return (clang_isConstQualifiedType(*(CXType*)d) ? CXChildVisit_Recurse : CXChildVisit_Break);
};
clang_visitChildren(cursor, RemoveConstFromType, &type);
}
I hope that helps. =)

multiple unique key in a multi index

I am trying to store objects in a boost multi-index container.
These objects are all unique can be retrieved by 2 separate keys (unique as well).
namepsace bm = boost::multi_index;
class MyObj {
string strid_;
int32_t numid_;
};
//! associative container searchable by ClOrdId and Sunofia Id.
typedef boost::multi_index_container< MyObj,
bm::indexed_by<
bm::ordered_unique<
bm::member<MyObj,string,&MyObj::strid_>
>,
bm::ordered_unique<
bm::member<MyObj,int32,&MyObj::numid_>
>
>
> Cntr;
Cntr cntr_;
When I try to find any element of that index by integer I use the following code
int32_t to_find = 12;
Cntr::iterator it = cntr_.find(id);
but it doesn't compile and I get the following error
error: invalid conversion from ‘int’ to ‘const char*’
When I use the same code with string it works fine; do you know what I am doing wrong?
auto it = cntr_.get<1>().find(id);
Each index is accessed separately (via get) and has its own member functions, iterators, etc. (In case you can't use auto,it is of type Cntr::nth_index<1>::type::iterator.) More info on the doc tutorial.

boost iterator facade and dereference() function

I'm trying to create an iterator which only can dereference to real value types, not to references.
Is this possible using boost::iterator_facade, or does it require me to have values that can be returned by adress\reference.
To be more specfic, my iterator returns a std::pair of references, which means my iterators value_type is not stored anywhere, but created on the fly on dereferencing (like std::map::iterator).
Yes, thing you want is possible. Please, take a look at boost/iterator_facade.hpp (example is for Boost lib of version 1.49.0 but it is ok for its new distributions also):
template <
class Derived
, class Value
, class CategoryOrTraversal
, class Reference = Value&
, class Difference = std::ptrdiff_t
>
class iterator_facade
Template argument Reference is the key. You should just specify Reference when deriving from boost::iterator_facade. For example, your code can look like as the following:
template<typename value_type>
class custom_iterator
: public boost::iterator_facade<
custom_iterator<value_type>,
value_type,
boost::forward_traversal_tag,
value_type
>
{
...
value_type dereference() const{ return value_type(...); }
...
};

Resources