gpt4 book ai didi

c++ - 模板类型函数的延迟评估

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:43 25 4
gpt4 key购买 nike

我读了“The C++ Programming language 4th edition, 1st printing, by Bjarne Stroustrup”这本书(来自 Amazon.com)。第 785 页。Stroustrup 正在解释他如何在使用“std::conditional + std::make_unsigned”时消除“::type”的显式书写,使用“类型别名”(关键字“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()
}

最佳答案

我认为 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

When a template-id refers to the specialization of an alias template, it is equivalent to the associated type obtained by substitution of its template-arguments for the template-parameters in the type-id of the alias template.

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

关于c++ - 模板类型函数的延迟评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20851788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com