【发布时间】:2013-12-31 04:39:12
【问题描述】:
我阅读了“The C++ Programming language 第 4 版,第 1 次印刷,Bjarne Stroustrup 所著”一书(来自 Amazon.com)。 第 785 页。 Stroustrup 正在解释他如何在使用“std::conditional + std::make_unsigned”时消除“::type”的显式书写,使用“type aliases”(关键字“using”)。但是在“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()
}
【问题讨论】:
-
字符串应该的错误是一个错误。
make_unsigned要求其模板参数是整数,但不是bool类型。据我了解,不使用嵌套的type并不在意。 -
你认为
Delay在做什么?书中的哪句话让你这么想?我的意思是,延迟评估可能会有所帮助,但是是什么让您认为Delay会这样做? -
@Yakk 我认为它应该延迟对元函数
Make_unsigned的评估,直到检查完类型是否为整数。 -
@DyP 当然,但除了它的名字,它为什么要这样做?
-
@Yakk Stroustrup 介绍了一个
conditional< is_integral<T>::value, make_unsigned<T>, Error<T> >::type并谈到使用它而不是typename make_unsigned<T>::type可以防止编译时错误。