延迟评估模板类型功能

vla*_*kov 6 c++ templates c++11

我读过"The C++ Programming language 4th edition,1st printing,by Bjarne Stroustrup"一书(来自Amazon.com).Stroustrup正在解释当使用" std :: conditional + std :: make_unsigned ",使用" type aliases "(关键字"using")时,他如何消除" :: type "的显式写法.但是在" std :: conditional + std :: make_unsigned " 上使用"类型别名"会导致编译错误.到现在为止,一切都应该如此.然后他继续展示如何使用"模板类型函数的延迟评估"来消除这些编译错误.

问题是在线Atype<make_unsigned<string>myType2<string> ....

我用过g ++ 4.8.2.

编译:

g ++ -std = c ++ 1y test45.cpp -oa

#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()
}
Run Code Online (Sandbox Code Playgroud)

dyp*_*dyp 3

我认为 Stroustrup 打算延迟对 的访问make_unsigned<T>::type,因为这个嵌套类型不是为非整数类型定义的。然而,对于 clang++ 和 g++ 来说,使用别名模板似乎还不够:它们Delay<Make_unsigned,T>直接解析为Make_unsigned<T>,而 this 解析为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...>;
Run Code Online (Sandbox Code Playgroud)

当然问题是,什么时候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;
}
Run Code Online (Sandbox Code Playgroud)

然而,别名模板的情况并非如此。他们被替换为 [temp.alias]/2

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

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