我读过Bjarne Stroustrup的《C++编程语言第4版,第1版》一书(来自Amazon.com)。第785页。Stroustrup解释了他如何在使用">std::conditional+std::make_unsigned"、使用">类型别名(关键字"using")时消除">:type的显式书写。但是,在">std::conditional+std::make_unsigned"上使用"类型别名"会导致编译错误。到目前为止,一切都是应该的。他接着展示了如何使用"模板类型函数的延迟求值"来消除这些编译错误。
问题在Atype<make_unsigned<string>
和myType2<string> ...
线上。
我使用g++4.8.2。
编译:
g++-std=c++1y测试45.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()
}
我认为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;
}
然而,别名模板的情况并非如此。它们被替换了[临时别名]/2
当模板id引用别名模板的专用化时,它等效于关联的类型通过将其模板参数替换为别名的类型id中的模板参数而获得样板
IMHO,这表明在上面的例子中,Delay<Make_unsigned,T>
等价于make_unsigned<T>::type
,将实例化make_unsigned<string>::type
并导致编译时错误。
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 >;
应该在T
上实际延迟应用template
,除非打字错误(在电话上)。