Circumventing template specialization

老子叫甜甜 提交于 2019-12-03 06:30:24

问题


Suppose I am a user of a Certain Template Library (CTL) which defines a template, named, say, Hector

template <class T>
class Hector {...};

And in its documentation it gives many guarantees about Hector template behavior. But then it also defines a specialization for a certain type Cool

template <>
class Hector<Cool> {....};

The purpose of the specialization is a more optimized implementation of Hector, but unfortunately because of this optimization many guarantees of Hector are violated.

Currently I really don't need the optimization, I'd rather preserve all the guarantees of Hector. Is there any way I could, without changing the library code (CTL is a highly respectable library, you know), circumvent the specialization? Any way at all? Maybe write some sort of wrapper? Anything? I just want to the compiler to generate code for Hector<Cool> in a normal, non-optimized way, with all the guarantees.


回答1:


Are you able to use the related template Reque that doesn't have the undesired specialization? Otherwise I think you'd need to create a wrapper for Cool so that the specialization isn't used.




回答2:


You could wrap cool in a dummy type to prevent the template from specializing it.




回答3:


No. And even if it can be done in some esoteric fashion, don't. Circumventing language features should set off an alarm.

You have to wrap the value or use a different type like char instead of bool (they behave similarly), giving std::vector<char> instead of std::vector<bool>.




回答4:


Here's a little generic disguiser:

template <typename T>
struct Drool
{
  Drool(T d) : b(d) { }
  inline operator T() const { return b; }
  inline Drool<T> & operator=(T d) { b = d; return *this; }
private:
  T b;
};

Now you can say Hector<Drool<Cool>>.


Improved version according to Xeo:

template <typename T>
struct Drool
{
  Drool(const T & d) : b(d) { }
  Drool(Drool && o) = default;

  inline operator const T & () const { return b; }
  inline operator       T & ()       { return b; }

private:
  T b;
};



回答5:


  1. Open the standard certain implementation
  2. Ctrl+A
  3. Ctrl+C
  4. Create a new file called "my_hector.h"
  5. Ctrl+V
  6. Remove the specialisation
  7. Search and replace #include <hector> with #include "my_hector.h"
    [ Edit for @Xeo ;-) ]
  8. Rename identifiers that begin with two leading underscores followed by a lowercase letter, and all identifiers that begin with a single leading underscore following by an uppercase letter.


来源:https://stackoverflow.com/questions/6499007/circumventing-template-specialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!