(非メンバー)関数で部分的なテンプレートの特殊化を使用しようとしていますが、構文につまずきます。StackOverflowで他の部分的なテンプレートの特殊化の質問を検索しましたが、それらはクラスまたはメンバー関数テンプレートの部分的な特殊化を扱っています。
出発点として、私は次のことを行っています。
struct RGBA {
RGBA(uint8 red, uint8 green, uint8 blue, uint8 alpha = 255) :
r(red), g(green), b(blue), a(alpha)
{}
uint8 r, g, b, a;
};
struct Grayscale {
Grayscale(uint8 intensity) : value(intensity) {}
uint8 value;
};
inline uint8 IntensityFromRGB(uint8 r, uint8 g, uint8 b) {
return static_cast<uint8>(0.30*r + 0.59*g + 0.11*b);
}
// Generic pixel conversion. Must specialize this template for specific
// conversions.
template <typename InType, typename OutType>
OutType ConvertPixel(InType source);
ConvertPixelを完全に特殊化して、RGBAからグレースケールへの変換関数を次のように作成できます。
template <>
Grayscale ConvertPixel<RGBA, Grayscale>(RGBA source) {
return Grayscale(IntensityFromRGB(source.r, source.g, source.b));
}
赤、緑、青を提供するピクセルタイプはもっとあると思いますが、おそらく異なる形式なので、私が本当にやりたいのは、さまざまなsを指定Grayscale
しOutType
、それでも許可することによる部分的な特殊化です。InType
私はこのようなさまざまなアプローチを試しました:
template <typename InType>
Grayscale ConvertPixel<InType, Grayscale>(InType source) {
return Grayscale(IntensityFromRGB(source.r, source.g, source.b));
}
しかし、(Microsoft VS 2008 C ++)コンパイラはそれを拒否します。
私が試みていることは可能ですか?もしそうなら、正しい構文は何ですか?