Ir ao conteúdo
  • Cadastre-se

C++11 - lambdas: como criar 1 lambda num parametro de 1 funçao?


Cambalinho
Ir à solução Resolvido por Cambalinho,

Posts recomendados

eis o construtor da minha class:

property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr)

agora ve esta class como teste:

class k{private:    string name;public:   property<string> Name([]()->string {return name;},    [](string strname)->void {name=strname;});};

o IDE da-me varios erros.. será que me enganei a escrever as lambdas como parametros?

os erros dizem que me esqueci '[' ou '['.. algo parecido. mas o que me podem dizer?

Link para o comentário
Compartilhar em outros sites

  • Solução

o problema estava na minha funçao propriedades e tinha de passar o 'this'(veja as macros Set() e Get()):

/*properties- how use create 1 property with macro:      PROPERTY(TypeName,PropertyName,getfunction, setfunction)- never forget to do 1 Copy Constructor inside of class's, that uses the property,         for avoid copy the 'this' value\adress and use a diferent memory adress*/template <typename T>class property{private:    T PropertyValue;    std::function<T(void)> getf;    std::function<void(T)> setf;public:    property(const T value)    {        getf=nullptr;        setf=nullptr;        PropertyValue=value;    };    property(const property &value)  :  PropertyValue(value.PropertyValue) , getf(value.getf)    {    }    property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr)    {        setf=SetFunction;        getf=GetFunction;    }    property& operator=(const T &value)    {        PropertyValue=value;        if (setf!=nullptr)            setf(value);        return *this;    }    property& operator=(const property &value)    {        PropertyValue = value.PropertyValue;        if (setf!=nullptr)            setf(PropertyValue);        return *this;    }    operator T()    {        if (getf!=nullptr)            return getf();        else            return PropertyValue;    }    friend ostream& operator<<(ostream& os, property& dt)    {        if(dt.getf==nullptr && dt.setf==nullptr)            os << dt.PropertyValue;        else if (dt.getf!=nullptr)            os << dt.getf();        return os;    }    friend istream& operator>>(istream &input, property &dt)    {        input >> dt.PropertyValue;        if (dt.setf!=nullptr)            dt.setf(dt.PropertyValue);        return input;    }    friend istream &getline(istream &in, property &dt)    {        getline(in, dt.PropertyValue);        if (dt.setf!=nullptr)            dt.setf(dt.PropertyValue);        return in;    }};template<typename T, typename Fnc1_t, typename Fnc2_t, typename classthis>property<T> GetProperty(Fnc1_t Getter, Fnc2_t Setter, classthis clsthis){    return property<T>(std::bind(Getter, clsthis), std::bind(Setter, clsthis, std::placeholders::_1));}#define PROPERTY(TypeName,PropertyName,getfunction, setfunction) \property<TypeName> PropertyName{std::bind(&getfunction, *this),std::bind(&setfunction, *this, std::placeholders::_1)}#define Get(x) [this]()->x#define Set(...) [this](__VA_ARGS__)->void

agora veja como a uso(com as macros):

class k{private:    string name;    int age;public:    property<string> Name    {        Get(string)        {            return name;        },        Set(string strname)        {            name=strname;        }    };    property<int> Age    {        Get(int)        {            return age;        },        Set(int intage)        {            age=intage;        }    };};

obrigado a todos ;)

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...