延迟评估模板类型功能

时间:2013-12-31 04:39:12

标签: c++ templates c++11

我读过“C ++编程语言第4版,第1版,Bjarne Stroustrup”一书(来自Amazon.com)。 第785页。 当使用“ std :: conditional + std :: make_unsigned ”时,Stroustrup正在解释他如何消除“ :: type ”的明确写作,使用“类型别名”(关键字“使用”)。但是在“ std :: conditional + std :: make_unsigned ”上使用“类型别名”会导致编译错误。 到现在为止,一切都应该如此。然后他继续展示如何使用“延迟评估模板类型函数”来消除这些编译错误。

问题在于Atype<make_unsigned<string>myType2<string> ...

我使用了g ++ 4.8.2。

编译:

g ++ -std = c ++ 1y test45.cpp -o a

#include <type_traits>
#include <string>
#include <iostream>
#include <typeinfo>  // for typeid(...)
using namespace std;

template<class T>
struct ErrIndicator {
   typedef ErrIndicator<T> type;
};

template<bool C, class T, class F>
using Conditional = typename conditional<C,T,F>::type;

template<typename T>
using Make_unsigned = typename make_unsigned<T>::type;

template<template<typename ...> class F, typename... Args>
using Delay = F<Args ...>;

template<class T>
using myType1 = Conditional<is_integral<T>::value,
    Make_unsigned<T>,
    ErrIndicator<T>
    >;

template<class T> 
    using myType2 = Conditional<is_integral<T>::value,
    Delay<Make_unsigned, T>,   // delayed evaluation
    ErrIndicator<T>
    >;

template<class T>
    using myType4 = Conditional<is_integral<T>::value,
    make_unsigned<T>,
    ErrIndicator<T>
    >;

template<typename T>
class Atype {}; 

template<typename T>
void func1(T &ia /* output param */) {
  cout << "unsigned integral type" << endl;
  ia = 4;  // "unsigned integral type" computation
}

template<typename T>
void func1(ErrIndicator<T> &) {
  cout << "non integral type: " << typeid(T).name() << endl;
}

int main() {
  myType1<int> var1a; // OK
  // myType1<string> var1b; // Error; The book says error
  //                    // should occur here. Here I understand.
  myType2<int> var2a; // OK
  // myType2<string> var2b; // Error - why?. Maybe I didn't get it,
  //                      // but I understand the book as no
  //                      // error should occur here.
  //                      // @DyP answered it.
  Atype<make_unsigned<string> > var3;  // OK here, look below at @DyP
  //                             // for "foo, bar, X" why
  //                        // make_unsigned<string> is not an error here.
  // make_unsigned<string> var6; // Error
  // Atype<make_unsigned<string>::type > var4;  // Error
  Atype<make_unsigned<int>::type > var5;  // OK
  //-------------
  myType4<string>::type var7;    // Look below for "myType3", where @Yakk
  //                          // obviates the necessity to write "::type".
  // rsl7 = 1:
  cout << "rsl7 = " << is_same<decltype(var7), ErrIndicator<string> >::value << endl; 
  func1(var7);  // "non integral type" overload of func1()
  //---------
  myType4<int>::type var8;
  // rsl8 = 1:
  cout << "rsl8 = " << is_same<decltype(var8), unsigned int>::value << endl; 
  func1(var8);  // "unsigned integral type" overload of func1()
}

2 个答案:

答案 0 :(得分:3)

我认为Stroustrup意图延迟对make_unsigned<T>::type的访问,因为这种嵌套类型没有为非整数类型定义。但是,对于clang ++和g ++,使用别名模板似乎不够:他们将Delay<Make_unsigned,T>直接解析为Make_unsigned<T>,将此解析为make_unsigned<T>::type

整个例子是:

template<typename C, typename T, typename F>
using Conditional = typename std::conditional<C,T,F>::type;

template<typename T>
using Make_unsigned = typename std::make_unsigned<T>::type;

// the example
Conditional<
  is_integral<T>::value,
  Delay<Make_unsigned,T>,
  Error<T>
>

// "The implementation of a perfect `Delay` function is nontrivial,
//  but for many uses this will do:"
template<template<typename...> class F, typename... Args>
using Delay = F<Args...>;

问题当然是Delay<Make_Unsigned,T>什么时候解决了?对于类模板(不是别名模板),只有在需要完整的对象类型或程序的语义受到影响时才会隐式实例化它们。考虑:

#include <type_traits>
using namespace std;

template<class T>
struct foo
{
    static_assert(is_same<T, void>{}, "!");
};

template<class X>
struct bar
{
    // without the line below, no error!
    //X x;
};

int main()
{
    bar<foo<int>> b;
}

然而,别名模板并非如此。他们被取代[temp.alias] / 2

  

template-id 引用别名模板的特化时,它等同于关联的类型   通过将 template-arguments 替换为别名的 type-id 中的 template-parameters 获得   模板。

恕我直言,这表明在上面的示例中,Delay<Make_unsigned,T>等同于make_unsigned<T>::type实例化make_unsigned<string>::type并导致编译时错误。

答案 1 :(得分:2)

template<bool, template<typename...>class Lhs, template<typename...>class Rhs, typename... Ts>
struct conditional_apply {
  typedef Lhs<Ts...> type;
};
template<template<typename...>class Lhs, template<typename...>class Rhs, typename...Ts>
struct conditional_apply<false, Lhs, Rhs, Ts...> {
  typedef Rhs<Ts...> type;
};
template<bool b, template<typename...>class Lhs, template<typename...>class Rhs, typename... Ts>
using Conditional_apply=typename conditional_apply<b, Lhs, Rhs, Ts...>::type;

template<class T> using myType3 = Conditional_apply<is_integral<T>::value, Make_unsigned, ErrIndicator, T >;

应该在template上实际延迟申请T,禁止打字错误(在电话上)。