【发布时间】:2022-01-18 05:14:09
【问题描述】:
我正在尝试创建一类静态模板方法:
#include<iostream>
#include<array>
#include<type_traits>
namespace InterpolationTypes
{
struct None{};
struct Linear{};
struct Lagrange3rd{};
struct Thirant{};
}
class Interpolation
{
public:
template <typename SampleType, typename InterpolationType>
static typename std::enable_if<std::is_same<InterpolationType, InterpolationTypes::None>::value, SampleType>::type
interpolation(SampleType samples[], int index, float frac)
{
return samples[index];
}
// template <typename SampleType, typename InterpolationType>
// static typename std::enable_if<std::is_same<InterpolationType, InterpolationTypes::Linear>::value, SampleType>::type
// interpolation(SampleType samples[], int index, float frac)
// {
// return frac * samples[index] + (1 - frac) * samples[index + 1];
// }
// template <typename SampleType, typename InterpolationType>
// static typename std::enable_if<std::is_same<InterpolationType, InterpolationTypes::Lagrange3rd>::value, SampleType>::type
// interpolation(SampleType samples[], int index, float frac)
// {
// return -1;
// }
// template <typename SampleType, typename InterpolationType>
// static typename std::enable_if<std::is_same<InterpolationType, InterpolationTypes::Thirant>::value, SampleType>::type
// interpolation(SampleType samples[], int index, float frac)
// {
// return -1;
// }
private:
Interpolation() = delete; // This class can't be instantiated, it's juce a holder for static methods..
};
template float Interpolation::interpolation<float, InterpolationTypes::None>;
int main(void)
{
float samples[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::cout<< "Interpolation type None -> " << Interpolation::interpolation<float, InterpolationTypes::None>(samples, 0, 0.5f) << std::endl;
// std::cout<< "Interpolation type Linear -> " << Interpolation::interpolation<float, InterpolationTypes::Linear>(samples, 0, 0.5f) << std::endl;
// std::cout << MyClass::MyMethod(MyEnum::First) << std::endl;
return 0;
}
喜欢这个。 我的目的是指定 SampleType 和 InterpolationType 接受哪些类型组合。不久前在网上阅读时,我记得有一个类似这样的解决方案
template float Interpolation::interpolation<float, InterpolationTypes::None>;
但是编译器给我的错误如下
g++ -std=c++14 enable_if.cpp
enable_if.cpp:47:31: error: explicit instantiation of 'interpolation' does not refer to a function template, variable template, member function, member class, or static data member
template float Interpolation::interpolation<float, InterpolationTypes::None>;
^
enable_if.cpp:18:2: note: explicit instantiation refers here
interpolation(SampleType samples[], int index, float frac)
^
1 error generated.
非常感谢任何帮助或提示。 祝你好运。
【问题讨论】: