如何使用C++ TMP实现MultiUnitValue类

NoS*_*tAl 6 c++ template-meta-programming

我在VC++中编写了这个类,这显然是非标准兼容的(TBH看起来很迟钝,标准不允许)所以

1.)我想知道如何将其转换为标准C++ 11

码:

#include <functional>
template <typename T, typename U, typename T_to_U >
class MultiUnitValue  //Multi for now == 2 :)
{
    const T t;
    T_to_U conversion_function;
public:
    MultiUnitValue()    : t(0)
    {}
    MultiUnitValue(T t_): t(t_)
    {}
    template<typename V>
    V in() const
    {
        BOOST_STATIC_ASSERT(0);// "you are trying to call in with type(unit) not supported"
    }
    template<>
    T in<T>() const
    {
        return t;
    }
    template<>
    U in<U>() const
    {
        return conversion_function(t);
    }


};
Run Code Online (Sandbox Code Playgroud)

用法:

auto f = [](int i){return i*2.54;};

MultiUnitValue<int, float,decltype(f)> muv(10);
auto rv = muv.in<float>();
Run Code Online (Sandbox Code Playgroud)

2.)我放置BOOST_STATIC_ASSERT以防止错误使用,
但看起来MSVC如果没有使用它就不会实例化(就像我期望的那样),但是当试图将它移植到g ++ 4.7时,即使模板没有实例化它也会被触发?用static_assert替换它不起作用......(为什么我不能用g ++中的穷人的概念:(:P)

有办法解决这个问题吗?

Pio*_*ycz 5

您的解决方案不符合"标准",因为成员函数不能专门用于类模板.这是因为一般规则函数不能部分专门化 - 因此即使是"完全"的成员函数模板专业化实际上也是部分专业化,因为没有完全专门化的类.


我的解决方案

C++ 11版

你的我的版本的例子,我相信这是你想要的:

int main(){
  auto f1 = [](int i){return i*2.54;};
  auto f2 = [](int i){ std::stringstream ss; ss << i; return ss.str(); };
  MultiUnitValue<int, float, std::string>  vv(1, f1, f2);
  std::cout << vv.in<int>() << "\n";
  std::cout << vv.in<float>() << "\n";
  std::cout << vv.in<std::string>() << "\n";
  // std::cout << vv.in<long>() << "\n"; // error to compile
}
Run Code Online (Sandbox Code Playgroud)

首先 - 您需要特殊的转换基类,对于单个转换,您将在下一个代码片段中看到,通过基类函数调用转换会导致"非指定"转换,这样long就不会编译.

template <class T, class U>
class Conversion {
public:
    Conversion(const std::function<U(const T&)>& f) : f(f) {}
    U convert (const T& v) const { return f(v); }
private:
   std::function<U(const T&)>  f;
};
template <class T>
class Conversion<T,T> {
public:
    T convert (const T& v) const { return v; }
};
Run Code Online (Sandbox Code Playgroud)

和你的类使用可变参数模板:

template <class T, class... V> // V... means all desired conversions
class MultiUnitValue : private Conversion<T,T>, private Conversion<T,V>... {
// allowed conversion:         ^^^^^^^^^^^^^^^^^        ^^^^^^^^^^^^^^^^^^
public:
   MultiUnitValue(T v, const std::function<V(const T&)>&... f) : Conversion<T,V>(f)..., v(v) {}

   template <class U>
   U in() const
   {
      // this static assert is not needed - but just to show the message
      static_assert(std::is_base_of<Conversion<T,U>, MultiUnitValue<T,V...>>::value, 
                   "Not allowed conversion");

      // static_assert is not needed 
      // since if you MultiUnitValue does not derive from Conversion<T,U> 
      // - then this call will not compile too
      return this->Conversion<T,U>::convert(v);
   }

private:
   T v;
};
Run Code Online (Sandbox Code Playgroud)

LVS示例:http://liveworkspace.org/code/05b6ada146cc8f05d027a5536859a087

没有可变参数模板的版本:

我还准备了没有可变参数模板的解决方案,因为VC++仍然不支持它们.

第二:转换和转换限制现在应该在你的T_to_U类型中.

使用这种方法,在与C++ 11版本的比较中使用会稍微不方便:

int main(){
  auto f1 = [](int i){return i*2.54;};
  auto f2 = [](int i){ std::stringstream ss; ss << i; return ss.str(); };
  // next 2 lines differ from C++11 version
  typedef ConvertFunctions2<int, float, std::string> CF_f1_f2; 
  MultiUnitValue<int, CF_f1_f2>  vv(1, CF_f1_f2(f1, f2));
  std::cout << vv.in<int>() << "\n";
  std::cout << vv.in<float>() << "\n";
  std::cout << vv.in<std::string>() << "\n";
  // std::cout << vv.in<long>() << "\n"; // error to compile
}
Run Code Online (Sandbox Code Playgroud)

MultiUnitValue将比您的示例更简单,即使从我的C++ 11版本更简单,但class CF会更复杂:

template <class T, class CF>
class MultiUnitValue {
public:
   MultiUnitValue(T v, const CF& cf) : v(v), cf(cf) {}

   template <class U>
   U in() const
   {
      return cf.Conversion<T,U>::convert(v);
   }

private:
   T v;
   CF cf;
};
Run Code Online (Sandbox Code Playgroud)

简单的"辅助"转换类与C++ 11版本中的完全相同:

template <class T, class U>
class Conversion {
   ...
};
template <class T>
class Conversion<T,T> {
   ...
};
Run Code Online (Sandbox Code Playgroud)

VC++中的可变参数模板替代(以及C++ 03的旧时代):

template <class T>
class ConvertFunctions0 : public Conversion<T,T> {};

template <class T, class V1>
class ConvertFunctions1 : public Conversion<T,T>, public Conversion<T,V1> {
public:
  ConvertFunctions1(std::function<V1(const T&)> f1) : Conversion<T,V1>(f1) {}
};

template <class T, class V1, class V2>
class ConvertFunctions2 : public Conversion<T,T>, public Conversion<T,V1>, public Conversion<T,V2> {
public:
  ConvertFunctions2(std::function<V1(const T&)> f1, std::function<V2(const T&)> f2) 
  : Conversion<T,V1>(f1), Conversion<T,V2>(f2) 
  {}
};
Run Code Online (Sandbox Code Playgroud)

正如你所看到的 - 添加ConvertFunctions3,ConvertFunctions4并不是那么大的麻烦......

ideone上的完整示例