简体   繁体   English

使用定义函数模板别名

[英]define function template alias by using

I define a function template alias:我定义了一个函数模板别名:

template <typename T>
using Cb = typename std::add_pointer<void(bool, T)>::type;

but got this error :但得到这个错误:

error: cannot convert 'Log::operator()(Read&) [with T = int]::' to 'Cb' {aka 'void (*)(bool, int)'} in assignment错误:无法在赋值中将 'Log::operator()(Read&) [with T = int]::' 转换为 'Cb' {aka 'void (*)(bool, int)'}

template <typename T>
class Log : public Sink<T> {
public:
    void
    operator()(Read<T> &read) {
        if (!more_) {
            // error !!!
            more_ = std::function<Cb<T>>([&](bool done, T val) {
                if (!done) {
                    cout << val << endl;
                    this->operator()(read);
                }
            });
        }
        read(false, more_);
    }

private:
    Cb<T> more_ = nullptr;
};

main function:主功能:

int main() {

    Log<int> logInt;

    return 0;
}

who to resolve this syntax error?谁来解决这个语法错误?

code example代码示例

Issue is that问题是

std::function<Cb<T>> resolve to std::function<void (*)(bool, int)> whereas you want std::function<Cb<T>>解析为std::function<void (*)(bool, int)>而你想要

std::function<void (bool, int)> . std::function<void (bool, int)>

So you might change your alias to所以你可能会改变你的别名

template <typename T>
using Cb = void(bool, T);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM