views:

76

answers:

3

I am trying to attach a function-object to be called on destruction of a templatized class. However, I can not seem to be able to pass the function-object as a temporary. The warning I get is (if the comment the line xi.data = 5;):

    warning C4930: 'X<T> xi2(writer (__cdecl *)(void))': 
    prototyped function not called (was a variable definition intended?)
            with
            [
                T=int
            ]

and if I try to use the constructed object, I get a compilation error saying:

error C2228: left of '.data' must have class/struct/union

I apologize for the lengthy piece of code, but I think all the components need to be visible to assess the situation.

template<typename T>
struct Base
{
    virtual void run( T& ){}
    virtual ~Base(){}
};

template<typename T, typename D>
struct Derived : public Base<T>
{
    virtual void run( T& t )
    {
        D d;
        d(t);
    }
};

template<typename T>
struct X
{
    template<typename R>
    X(const R& r)
    {
       std::cout << "X(R)" << std::endl;
       ptr = new Derived<T,R>(); 
    }

    X():ptr(0)
    { 
        std::cout << "X()" << std::endl; 
    }

    ~X()
    {
        if(ptr) 
        {
            ptr->run(data);
            delete ptr;
        }
        else
        {
            std::cout << "no ptr" << std::endl;
        }
    }

    Base<T>* ptr; 
    T data;
};

struct writer
{
    template<typename T>
    void operator()( const T& i )
    { 
        std::cout << "T : " << i << std::endl;
    }
};

int main()
{
    {
        writer w;
        X<int> xi2(w);
        //X<int> xi2(writer()); //This does not work!
        xi2.data = 15;       
    }

    return 0;
};

The reason I am trying this out is so that I can "somehow" attach function-objects types with the objects without keeping an instance of the function-object itself within the class. Thus when I create an object of class X, I do not have to keep an object of class writer within it, but only a pointer to Base<T> (I'm not sure if I need the <T> here, but for now its there).

The problem is that I seem to have to create an object of writer and then pass it to the constructor of X rather than call it like X<int> xi(writer();

I might be missing something completely stupid and obvious here, any suggestions?

+4  A: 

Try an extra pair of brackets around X<int> xi2((writer())); This will stop the compiler thinking your predeclaring a function. (Scott Meyers Effective STL Item 6.)

Charles Beattie
Thanks for the Book reference.
Akanksh
+6  A: 

Looks like a "most vexing parse" issue. Try

X<int> xi2 = X<int>(writer());

or

X<int> xi2((writer()));
Tyler McHenry
Nice, both of these work! But its sad that the beauty of using the function-object "when needed" is lost (or requires extra brackets or explicit calls to the constructor). Thanks! ( oh! and I had never heard of the "most vexing parse", so thats new :) )
Akanksh
+4  A: 

X<int> xi2(writer()); is a declaration of a function called xi2 which itself returns an X<int>, and takes as parameter a function that takes no parameters and returns a writer. It's a "most vexing parse".

The solution is either to do what you've done, avoiding the temporary, or to add more parentheses.

Steve Jessop