2016-04-26 40 views
1

テンプレート関数のシグネチャは2つあります。ここで、Tはintまたはdoubleのいずれかになります。テンプレート関数への関数ポインタ

template <typename T> 
Box<T> f2p(Box<T> const& box, Point<T> const& pt, Orientation o) 
{ 
... 
} 

template <typename T> 
Box<T> p2f(Box<T> const& box, Point<T> const& pt, Orientation o) 
{ 
... 
} 

ここでは、方向に応じて、f2pまたはp2fのいずれかを呼び出したいとします。私はf2pまたはp2fのいずれかを指す関数ポインタを作成したい。テンプレート関数への関数ポインタを作成するにはどうすればよいですか?私はこのような何かを試してみたが、私はコンパイルエラーを取得

typename <template T> 
Box<T> do_transformation(Box<T> const& box, ..., int dir = 0) 
{ 
    function pointer p = dir ? pointer to f2p : pointer to p2f 

    return p<T>(box); 
} 

:私は、次のような効果を実現したい

Box<T> (*p)(Box<T>, Point<T>, Orientation) = dir ? fc2p<T> : p2fc<T> 

答えて

2

私はこのような何かをしようと、私はコンパイルエラーを取得:

Box<T> (*p)(Box<T>, Point<T>, Orientation) = dir ? f2p<T> : p2f<T> 

は、あなたの関数が取る引数で慎重に見てみましょう:

template <typename T> 
Box<T> f2p(Box<T> const& box, Point<T> const& pt, Orientation o) 
       ^^^^^^^^    ^^^^^^^^ 

すべての引数と正確に一致する必要があります。この場合:

Box<T> (*p)(Box<T> const&, Point<T> const&, Orientation) = dir ? f2p<T> : p2f<T>; 

それとも、単に:

auto p = dir ? f2p<T> : p2f<T>; 
+0

はそれもautoを使用するために私には発生しなかったと信じてすることはできません。 –

0

あなたが何か他のもののためにpointerを必要とする場合を除き、あなたが使用することができます。

typename <template T> 
Box<T> do_transformation(Box<T> const& box, ..., int dir = 0) 
{ 
    return (dir ? f2p(box, ...) : p2f(box, ...)); 
} 
2

関数テンプレートへのポインタを持つことはできませんが、関数テンプレートの特定のインスタンスへのポインタを持つことができます。

Box<T>(*p)(Box<T> const&, Point<T> const&, Orientation); 
p = dir ? &f2p<T> : &p2f<T>; 
関連する問題