簡體   English   中英

具有invoke_result的重載命名非成員函數的返回類型

[英]Return type of overloaded named non-member function with invoke_result

給定許多帶有不同類型參數的重載函數,是否有辦法在編譯時在模板化上下文中獲取特定重載的返回類型或參數類型之一? 例如,考慮以下情況,其中重載函數將參數引用作為輸出值:

struct struct_a { };
struct struct_b { };
struct struct_c { };

float process(struct_a) { return 5.0f;  }
bool  process(struct_b) { return true; }
int   process(struct_c) { return -1; }

void process_in_place(struct_a, float& out) { out = 5.0f;  }
void process_in_place(struct_b, bool& out) { out = true;  }
void process_in_place(struct_c, int& out) { out = -1;  }

template<typename T>
void do_process(T val)
{
    auto result1 = process(val);
    std::cout << result1 << std::endl;

    // ???
    //using post_process_type = std::invoke_result_t<decltype(process)&(std::declval<T>)>;
    // ???
    post_process_type result2;
    process_in_place(val, result2);
}

int main()
{
    do_process(struct_a());
    do_process(struct_b());
    do_process(struct_c());
}

在最終選擇哪個版本的process_in_place之前,我怎么知道out-param應該是哪種類型?

using post_process_type = decltype(process(std::declval<T>()));

這樣就可以了。 在重載解析選擇了正確的process之后, decltype只會查看返回類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM