samedi 1 août 2015

Inline function is not defined

Using Variant and Visitor idioms for dealing with abstract syntax tree of mathematical expression I faced linker errors undefined reference to 'function_name' prepended with warnings inline function 'function_name' is not defined [-Wundefined-inline], where function_name is all possible names of specializations (i.e. for B and for C) of function marked // 1 below (header):

#pragma once
#include <utility>
#include <iostream>

#include <boost/variant.hpp>

struct A {};
struct B {};
struct C {};

using V = boost::variant< A, B, C >;

struct visitor
{

    using result_type = void;

    result_type
    operator () (V v) const
    {
#if 1
        return boost::apply_visitor([this] (auto && x) -> result_type { return apply(std::forward< decltype(x) >(x)); }, v);
    }
#else
        return boost::apply_visitor(*this, v);
    }

    template< typename T >
    result_type
    operator () (T && x) const
    {
        return apply(std::forward< T >(x));
    }
#endif

private :

    result_type
    apply(A) const
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }

    template< typename T >
    result_type
    apply(T &&) const
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }

public : // !

};

(main.cpp):

#include "implementation.hpp"

#include <cstdlib>

int
main()
{
    visitor{}(V{B{}});
    return EXIT_SUCCESS;
}

Here is live example: main.cpp and header files. If contents of header file moved to main.cpp, then warning changed to function 'function_name' has internal linkage but is not defined [-Wundefined-internal]. But I am sure the sources of errors are exactly the same.

If I change #if 0 to #if 1, then all compiles fine. Another note is: when I move result_type operator () (V v) const to second public section (marked // !), then all compiles fine too.

Seems lambda makes something subtle with instantiation of function template defined strictly below definition of lambda.

How to explain above behaviour and difference in behaviour of lambda version and version with visitor & passed to boost::apply_visitor?

Real-life example is here.

How to pull out and get an item from a std::vector

Let's say I have a vector of MyObject*. I would like to pull out an item from the vector and get that item (i.e. not using erase or pop_back because it deletes the pointer).

So, in code, is there an easy way to to this ?

std::vector<MyObject *> items;

MyObject *takeAt(unsigned int index) {
    // find item, then take it off from the list
    return item;
}

I have ideas, like using std::transform to generate a new vector without this particular item, and then set this new vector. But has anyone a better idea ?

Inline function is not defined

Using Variant and Visitor idioms for dealing with abstract syntax tree of mathematical expression I faced linker errors undefined reference to 'function_name' prepended with warnings inline function 'function_name' is not defined [-Wundefined-inline], where function_name is all possible names of specializations (i.e. for B and for C) of function marked // 1 below (live example):

#include <type_traits>
#include <utility>
#include <iostream>

#include <boost/variant.hpp>

#include <cstdlib>

namespace
{

struct A {};
struct B {};
struct C {};

using V = boost::variant< A, B, C >;

struct visitor
{

    using result_type = void;

    result_type
    operator () (V v) const
    {
#if 1
        return boost::apply_visitor([this] (auto && x) -> result_type { return apply(std::forward< decltype(x) >(x)); }, v);
    }
#else
        return boost::apply_visitor(*this, v);
    }

    template< typename T >
    result_type
    operator () (T && x) const
    {
        return apply(std::forward< T >(x));
    }
#endif

private :

    result_type
    apply(A) const
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }

    template< typename T >
    result_type
    apply(T &&) const // 1
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }

public : // !

};

}

int
main()
{
    visitor{}(V{B{}});
    return EXIT_SUCCESS;
}

Frankly, warning message here is function 'function_name' has internal linkage but is not defined [-Wundefined-internal], but if we split above code into header and .cpp files, then message would be exactly as one mentioned above.

If I change #if 0 to #if 1, then all compiles fine. Another note is: when I move result_type operator () (V v) const to second public section (marked // !), then all compiles fine too.

Seems lambda makes something subtle with instantiation of function template defined strictly below definition of lambda.

How to explain above behaviour and difference in behaviour of lambda version and version with visitor & passed to boost::apply_visitor?

Real-life example is here.

C++ Accessing Class Pointer Stored In Map

Here's my code:

class BCConVar
{
public:
    BCConVar ( ) { }
};

template<class T>
class CConVar : public BCConVar
{
    T& m_cvar;
public:
    T& Cvar ( ) const { return m_cvar; }
    void Cvar ( T& val ) { m_cvar = val; }
    CConVar ( T& );

};

class CCvars
{
public:
    std::map<std::string, BCConVar*> cVars;
};

I initialize it like:

 cVars.insert ( std::make_pair ( std::string ( "test1" ), new CConVar<float> ( pCvars->settings.test ) ) );

How can I access pointer stored in map? Visual Studio is showing me hints that I should write cVars[cvar ].BCConVar but that's wrong, how can I access pointer stored under key X ?

delete a node at nth position IN C++

please i am having problems deleting from any position given in a linked list. i had done it for the first position and last position of my list and it works fine but tried it for nth position but it gives me run time error please whats wrong with my code. here the code below, thanks for your help in advance

class node{
public:
        int number;
        node *next;
};
class Box{
public:
void insertAsFirstElement(node *&head, node *&last,int number);
void insert(node *&head, node *&last,int number);
void remove(node *&head, node *&last);
void showList(node *current);
};
bool isEmpty(node *head)
{
    if(head == NULL)
        return true;
    else
        return false;
}
char menu()
{
    char choice;
    cout<<"Menu"<<"1. Add an item.\n"<<"2. Remove an item.\n"<<"3. Show the list.\n"<<"Exit\n";
    cin >> choice;
    return choice;


}
void insertAsFirstElement(node *&head, node *&last,int number)
{
    node *temp = new node;
    temp->number = number;
    temp->next = NULL;
    head = temp;
    last = temp;

}
void insert(node *&head, node *&last,int number)
{
    if(isEmpty(head))
        insertAsFirstElement(head, last, number);
    else
    {node *temp = new node;
    temp->number = number;
    temp->next = NULL;
    last->next = temp;
    last = temp;

    }
}
void remove(node *&head, node *&last,int number)
{
    cin>>number;
    node *temp = new node;
    if(isEmpty(head))
        cout<< "this list is already empty.\n";
    else if (head == last)
    {

    delete head;
    head = NULL;
    last = NULL;
    }


    else
    {for(int i = 0; 1< number-2; i++)
    /*node *temp = head;
    head = head->next;
    delete temp;*/
    node *temp = head;
    head = head->next;
    temp= temp->next;
    node* temp2 = temp->next;
    temp->next = temp2->next;
    delete temp2;
    }
}
void showList(node *current)
{ if(isEmpty(current))
 cout << "The list is empty\n";
 else{
 cout << "The list contains:\n";
 while(current != NULL)
 {cout << current->number << endl;
 current = current->next;
 }}}


int main(){
    node *head = NULL;
    node *last = NULL;
    char choice;
    int number;
    do{
        choice = menu();
         switch (choice)
         {
         case'1': cout << "please enter a number: ";
             cin >> number;
             insert (head,last,number);
             break;
         case'2': remove(head,last,number);
             break;
         case '3': showList(head);
             break;
         default: cout<< "System exit\n";
         }

    }while (choice !='4');
}

getting this type in object declaration context

As for the research I've done there is no good method to get the type of the enclosing class in static context. What about non-static context.

example:

template<typename TOwner, typename T>
struct prop {
    typedef TOwner Owner;

    prop(TOwner* owner) {
    }
};

struct MyType {
    prop<MyType, short> my_prop = {this};
    prop<decltype(my_prop)::Owner, long> prp = {this};
};

This code works, great. But try to replace MyType with decltype(*this) and it stops working saying this can not be declared in a static context. However my_prop can not be considered a static context as well and is more or less in the same context as this (can be rephrased as this-> my_prop).

So my question is following: am I missing something or there really is no way to get type of this even in non-static context while declaring property?

What are the standard functions that every C++ programmer should know?

As someone who is a beginner to the language, I often find that there are functions which I am unaware about when I am writing code. Perhaps you know of a resource that has all the standard functions in a C++ programmer's arsenal?