mercredi 2 mai 2018

Using decltype like typedef

Experimenting with decltype to be used in place of a typedef or auto keywords whenever deemed necessary. Some of them ( stl and user function ) works however it doesn't work directly with a user defined object. Please I need an explanation here.

#include <iostream>
#include <vector>

using namespace std;

struct foo {
    void operator=(const int a ) { val_ =a;}
    int get() const { return val_;}
    private:
    int val_;
}; 

ostream& operator << (ostream& os, foo f )
{
    os << f.get();
    return os;
}

foo func()
{
    foo a;
    return a;
}

int main()
{
    vector<foo> foos;
    decltype(foos) b; //works

    decltype(foo);//  don't work  - says - error C3553: decltype expects an expression not a type

    decltype(func()) a; //works
    a = 100;

    std::cout << a << " \n";
}

Aucun commentaire:

Enregistrer un commentaire