将匿名临时函数对象传递给模板化构造函数时出现问题

发布于 2024-08-28 00:52:48 字数 1984 浏览 13 评论 0原文

我正在尝试附加一个在模板化类销毁时调用的函数对象。但是,我似乎无法将函数对象作为临时对象传递。我得到的警告是(如果注释行 xi.data = 5;):

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

如果我尝试使用构造的对象,我会收到一个编译错误:

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

我为冗长的一段内容表示歉意代码,但我认为所有组件都需要可见才能评估情况。

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;
};

我尝试这样做的原因是,我可以“以某种方式”将函数对象类型与对象附加在一起,而无需在类中保留函数对象本身的实例。因此,当我创建class X的对象时,我不必在其中保留class writer的对象,而只需在其中保留一个指向Base的指针。 (我不确定这里是否需要 ,但现在它就在那里)。

问题是我似乎必须创建一个 writer 对象,然后将其传递给 X 的构造函数,而不是像 X那样调用它。 xi(writer();

我可能会在这里遗漏一些完全愚蠢和明显的东西,有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤者何惧 2024-09-04 00:52:48

看起来像是一个“最令人烦恼的解析”问题。尝试

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

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

Looks like a "most vexing parse" issue. Try

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

or

X<int> xi2((writer()));
海拔太高太耀眼 2024-09-04 00:52:48

X; xi2(writer()); 是一个名为 xi2 的函数的声明,该函数本身返回一个 X,并采用一个不带参数并返回 writer 的函数作为参数。这是一个“最令人烦恼的解析”。

解决方案是要么做你已经做过的事情,避免临时的,要么添加更多括号。

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.

东北女汉子 2024-09-04 00:52:48

尝试在 X周围加上一对括号xi2((writer())); 这将阻止编译器认为您预先声明了一个函数。 (斯科特·迈耶斯有效 STL 第 6 项。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文