English 中文(简体)
c++ 如何减少相同模板专业的分布?
原标题:c++ how to reduce the amout of identical template specializations?

I have this functor:

struct functor
{
  template<class T> void operator()(T value)           // (*)
  {
    // process the value
  }
  template<> void operator()<const wchar_t *>(const wchar_t *value) // (**)
  {
    if (value)
    {
      // process the value
    }
  }
  template<> void operator()<const char *>(const char *value) // (**)
  {
    if (value)
    {
      // process the value
    }
  }
  template<> void operator()<wchar_t *>(wchar_t *value) // (**)
  {
    if (value)
    {
      // process the value
    }
  }
  template<> void operator()<char *>(char *value) // (**)
  {
    if (value)
    {
      // process the value
    }
  }
};

你可以看到,我有4个相同的模板专业。 是否有办法一一一一一一一一一一一一一一一一一一一一一一一一一一一一,即一一一how,就可将所有可能的类型分割至主要类别(*)和专门类别(**)?

感谢。

http://www.ohchr.org。

Oops, 固定了一些类型。

最佳回答

你们可以放弃一个更简单的计划——超负荷工作!

template<class T>
void foo(T value){ // general
  // ...
}

template<class T>
void foo(T* value){ // for pointers!
  if(value)
    foo(*value); // forward to general implementation
}

Also, I d recommend to make the parameter a reference - const if you don t need to modify it (or maybe both, depending on what you actually need to do):

template<class T>
void foo(T& value){ // general, may modify parameter
  // ...
}

template<class T>
void foo(T const& value){ // general, will not modify parameter
  // ...
}

如果你想对某几类(即整个一套)实施特殊规定,则海峡和标签的发送可以帮助你:

// dispatch tags
struct non_ABC_tag{};
struct ABC_tag{};

class A; class B; class C;

template<class T>
struct get_tag{
  typedef non_ABC_tag type;
};

// specialization on the members of the set
template<> struct get_tag<A>{ typedef ABC_tag type; };
template<> struct get_tag<B>{ typedef ABC_tag type; };
template<> struct get_tag<C>{ typedef ABC_tag type; };

// again, consider references for  value  - see above
template<class T>
void foo(T value, non_ABC_tag){
  // not A, B or C ...
}

template<class T>
void foo(T value, ABC_tag){
  // A, B, or C ...
}

template<class T>
void foo(T value){
  foo(value, typename get_tag<T>::type()); // dispatch
}

底线是,如果你想要把没有共同之处的类型分类,那么你就至少需要一些重复(标签、超负荷......)。

问题回答

You mean like this?

struct functor
{
    template<class T> void operator()(T value)
    {
        // process the value
    }
    template<class T> void operator()(T* value) // overload, not specialization
    {
        if (value) {
            // process the value
        }
    }
};

rel=“nofollow”>http://ideone.com/P8GLp

If you want only those types, something else

struct functor
{
protected:
    template<class T> void special(T* value) // overload, not specialization
    {
        if (value) {
            // process the value
        }
    }    
public
    template<class T> void operator()(T value)
    {
        // process the value
    }
    void operator()(char* value) {special(value);}
    void operator()(wchar_t* value) {special(value);}
    void operator()(const char* value) {special(value);}
    void operator()(const wchar_t* value) {special(value);}
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签