下の(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
}
> (accumulateに渡すaryからtemplate_mulの型推論は出来ないのか)
できねぇっす。
>(3)の書き方は、C++の仕様に含まれた正しい書き方でしょうか?
はい。正しいです。
シャノンさん、keichanさん、ありがとうございます。
ツイート | ![]() |