简体   繁体   English

如何让这个模板类编译为类和基元类型?

[英]How do I get this template class to compile for Classes and primitive types?

How do I get the following code to compile? 如何编译以下代码?

I realize that the compiler is not happy with V<double> because it tries to compile my typedef for GetterFn , but I would like GetterFn and GetCalc() to be available for classes, but ignored for primitive types. 我意识到编译器对V<double>不满意,因为它试图为GetterFn编译我的typedef,但我希望GetterFnGetCalc() 可用于类,但忽略了原始类型。

How should I recode this class? 我该如何重新编写这门课程?

#include <vector>

using namespace std;

class Bar
{
    public:
        float getMyFloat() const { return 42.5; }
};

template< typename T >
class V
{
    public:
        typedef float (T::*GetterFn)() const;
        void getCalc( std::vector<double>& vec, GetterFn fn ) const
        {
            vec.clear();
            for ( size_t i=0; i<m_v.size(); ++i )
                vec.push_back( m_v[ i ].*(fn)() );
        }

    private:
        vector<T> m_v;
};

int main(int argc, char** argv)
{
    V<Bar>    vb;  // ok
    V<double> vd;  // compiler not happy
}

Alf's slightly snarky answer is indeed the easiest way. 阿尔夫略带讽刺的回答确实是最简单的方法。 Instead of having the GetterFn be a member function pointer, just use an overloaded free function to get the value: 不要让GetterFn成为成员函数指针,只需使用重载的自由函数来获取值:

void getCalc( std::vector<double>& vec) const
{
    vec.clear();
    for ( size_t i=0; i<m_v.size(); ++i )
        vec.push_back( get_value(m_v[ i ]) );
}

and then just overload get_value appropriately: 然后恰当地重载get_value

double get_value(double value) { return value; }

double get_value(Bar value) { return value.getMyFloat(); }

Of course, a more descriptive name than get_value is highly recommended. 当然,强烈建议使用比get_value更具描述性的名称。

A double cannot have a member function. double精灵不能有会员功能。 Ergo, use a non-member function. 因此,使用非成员函数。

It's a bit hard to suggest the complete solution without knowing what you want to do for fundamental types. 如果不知道你想要为基本类型做什么,建议完整的解决方案有点困难。 However, at the heart I suggest you employ the std::is_fundamental type trait. 但是,我建议您使用std::is_fundamental类型特征。

If you want entire collections of member functions to exist only conditionally, then perhaps wrapping them into a member helper class is one idea: 如果您希望整个成员函数集合仅有条件地存在,那么将它们包装到成员助手类中是一个想法:

#include <vector>
#include <type_traits>
#include <cstddef>

class Bar
{
public:
  float getMyFloat() const { return 42.5; }
};

template< typename T >
class V
{
private:

  static const bool m_primitive = std::is_fundamental<T>::value; // convenience

  template <bool B, typename U> struct Helper;

  template <typename U> struct Helper<false, U>
  {
    typedef float (T::*GetterFnType)() const;
    void getCalc(std::vector<double> & v1, std::vector<U> const & v2, GetterFnType f)
    {
      v1.clear();
      for (std::size_t i = 0; i < v2.size(); ++i)
        v1.push_back((v2[i].*f)());
    }
  };

public:

  // use Helper<m_primitive, T>::GetterFn and Helper<m_primitive, T>::getCalc() here

private:
  std::vector<T> m_v;
};

int main(int argc, char** argv)
{
  V<Bar>    vb;  // ok
  V<double> vd;  // compiler also happy
}

You'll probably also have to do some std::enable_if in the actual implementation. 您可能还需要在实际实现中执行一些std::enable_if If you post more details, we can elaborate. 如果您发布更多详细信息,我们可以详细说明。

If you're open to a broader redesign, Konrad's answer feels like the final result would be somewhat cleaner and simpler, but then again I'm all in favour of convoluted template voodoo :-) 如果你对更广泛的重新设计持开放态度,康拉德的回答感觉最终结果会更清晰,更简单,但我再次赞成复杂的模板伏都教:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c ++模板:如何在类和原始类型之间动态选择 - c++ template: How to dynamically choose between classes and primitive types 如何获得此模板类进行编译? - How to get this template class to compile? 如何在可变参数模板类中获取函数指针的参数类型? - How do I get the argument types of a function pointer in a variadic template class? 如何检查 c++ 中模板 class 中的类型? - How do I check types within a template class in c++? 如何将模板类限制为某些内置类型? - How do I restrict a template class to certain built-in types? 如何复制包含非原始类型的数组? - How do I copy arrays that contain non primitive types? 如何强类型定义非基本类型? - How do I strongly typedef non-primitive types? 如何仅通过其中一种类型引用具有多种模板类型的 class? - How do I refer to a class with multiple template types by only one of the types? Java:如果我使用装在类中的原始数据类型,速度会慢多少? - Java: How much slower would it be if I used primitive data types boxed in classes? 即使基础和派生 class 仅使用原始数据类型,我是否需要定义虚拟析构函数? - Do I need to define a virtual destructor even if the base and derived class only use primitive data types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM