Circumventing template specialization

安稳与你 提交于 2019-12-02 20:05:44

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.

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

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>.

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