テンプレート関数を引数として渡すときの型推論

解決


pen  2007-11-25 22:36:48  No: 66990  IP: 192.*.*.*

下の(1),(2),(3)について質問なのですが、

・(2)がコンパイルエラーになるということは、テンプレート関数は
  (3)のように明示的にその型を指定しないかぎり、
  関数の引数として渡すことは出来ないということですか?
  (accumulateに渡すaryからtemplate_mulの型推論は出来ないのか)

・(3)はg++3.4.4 cygwin,  bcc32で問題なくコンパイルできました。
  (3)の書き方は、C++の仕様に含まれた正しい書き方でしょうか?
  (調べても良く分かりませんでした)

g++3.4.4 cygwin

#include <iostream>
#include <numeric>
using namespace std;

int mul(int x, int y) { return x * y; }

template <class T>
T template_mul(T x, T y) { return x * y; }

int main() {
  int ary[] = { 255, 2, 2 };

  accumulate(ary, ary+3, 1, mul); // (1) コンパイルOK
  accumulate(ary, ary+3, 1, template_mul); // (2) コンパイルエラー
  // no matching function for call to `accumulate(int[3], int*, int, <unknown type>)'
  accumulate(ary, ary+3, 1, template_mul<int>); // (3) コンパイルOK
}

編集 削除
シャノン  2007-11-26 01:31:23  No: 66991  IP: 192.*.*.*

>   (accumulateに渡すaryからtemplate_mulの型推論は出来ないのか)

できねぇっす。

編集 削除
keichan  2007-11-27 14:10:50  No: 66992  IP: 192.*.*.*

>(3)の書き方は、C++の仕様に含まれた正しい書き方でしょうか?
はい。正しいです。

編集 削除
pen  2007-11-28 00:59:49  No: 66993  IP: 192.*.*.*

シャノンさん、keichanさん、ありがとうございます。

編集 削除