1

呼び出し可能なオブジェクト (関数)aと引数(または一連の引数) が与えられた場合、複数のシグネチャでオーバーロードされていることを考慮して、b返される型を推測したいと思います。ff

私の多くの試みの1つは

#include <iostream>
#include <cstdint>
#include <string>
#include <functional>
#include <utility>
#include <typeinfo>

int foo(uint32_t a) { return ((a + 0) * 2); }

bool foo(std::string a) { return (a.empty()); }

/*template <typename A, typename B> auto bar(A a, B b) -> decltype(a(b)) {
  return (a(b));
}*/

/*template <typename A, typename B> decltype(std::declval<a(b)>()) bar(A a, B b)
{
  return (a(b));
}*/

template <typename A, typename B> void bar(std::function<A(B)> a, B b) {
  std::cout << a(b) << "\n";
}

int main() {

  // the following 2 lines are trivial and they are working as expected
  std::cout << foo(33) << "\n";
  std::cout << typeid(decltype(foo(std::string("nothing")))).name() << "\n";

  std::cout << bar(foo, 33) << "\n";
  //std::cout << bar(foo, std::string("Heinz")) << "\n";

  return (0);
}

および 2 つのテンプレート オプションがコメント アウトされ、前のコードに含まれています。

何気なく使ってdeclval result_of auto decltypeます。

オーバーロード解決プロセスはコンパイル時にどのように機能しますか?

私がこれでクリエイティブになろうとしている理由を知りたい人がいれば、C++11 で実行可能な/きちんとした方法でカリー化を実装しようとしているということです。

4

3 に答える 3

3

foo がオーバーロードされている場合は、次を使用する必要があります。

#include <type_traits>

int foo(int);
float foo(float);

int main() {
    static_assert(std::is_same<decltype(foo(std::declval<int>())), int>::value, "Nope.");
    static_assert(std::is_same<decltype(foo(std::declval<float>())), float>::value, "Nope2.");
}

そうでない場合は、これで十分です。

#include <type_traits>
bool bar(int);

int main() {
    static_assert(std::is_same<std::result_of<decltype(bar)&(int)>::type, bool>::value, "Nope3.");
}

はい、暗黙のアドホックオーバーロードが何をするかを明示的に抽出しようとしているため、冗長です。

于 2013-12-06T00:26:06.947 に答える