开发者

How to call a templated operator overload without using the word 'operator'?

开发者 https://www.devze.com 2022-12-31 10:16 出处:网络
class RunAround; class HopUpAndDown; class Sleep; template<typename Acts> int doThis(); template<> int doThis<RunAround>(){ /* run run run.. */ return 3; }
class RunAround;
class HopUpAndDown;
class Sleep;

template<typename Acts> int doThis();
template<> int doThis<RunAround>()    { /* run run run.. */ return 3; }
template<> int doT开发者_开发知识库his<HopUpAndDown>() { /* hop hop hop.. */ return 2; }
template<> int doThis<Sleep>()        { /* zzz.. */ return -2; }

struct Results
{   
    template<typename Act> int& operator()()
    {
        static int result;
        return result;
    }
};



int main()
{
    Results results;
    //results<RunAround>() = doThis<RunAround>();
    results.operator ()<RunAround>() = doThis<RunAround>();
    results.operator ()<Sleep>() = doThis<Sleep>();
    return 0;
};

If I remove the comment, the compiler thinks I am calling operator() in non-existant template class Results<RunAround> when I want operator<RunAround>() in class Results.

If I want to continue using an operator overload instead of a normal name, am I doomed to use the awful syntax below the comment (which does work)?


The most comfortable thing is to let template argument deduction work for you:

struct Results {   
    template<typename Act> int& operator()(Act) { /* ... */ }
};

results(RunAround()) = /* ... */;
0

精彩评论

暂无评论...
验证码 换一张
取 消