久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++17類模板部分推導

C++17 class template partial deduction(C++17類模板部分推導)
本文介紹了C++17類模板部分推導的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我對模板的理解類模板的參數推導 提議是在推導上下文中將模板函數和模板類的行為同質化.但我覺得我誤解了一些東西.

My understanding about the Template argument deduction for class templates proposal was to homogenize the behaviour of template functions and template classes in deduction contexts. But I think that I have misunderstood something.

如果我們有這個模板對象:

If we have this template object:

template <std::size_t S, typename T>
struct test
{
    static constexpr auto size = S;
    using type_t = T;

    test(type_t (&input)[size]) : data(input) {}
    type_t (&data)[size]{};
};

我傾向于使用輔助函數作為語法糖來創(chuàng)建test對象:

I tend to use a helper function as syntactic sugar for creating test objects:

template <std::size_t S, typename T>
test<S, T> helper(T (&input)[S]) { return input; }

如下圖所示:

int main()
{
    int buffer[5];

    auto a = helper<5, int>(buffer); // No deduction
    auto b = helper<5>(buffer);      // Type deduced
    auto c = helper(buffer);         // Type and size deduced

    std::cout << a.size << b.size << c.size;

    return 0;
}

上面的代碼按預期輸出 555.我使用較新的編譯器設置在 Wandbox 中嘗試了相同的操作1:

The code above outputs 555 as expected. I've tried the same in Wandbox using the newer compiler setup1:

int main()
{
    int buffer[5];

    test<5, int> a(buffer); // No deduction: Ok.
    test<5> b(buffer);      // Type deduced: FAILS.
    test c(buffer);         // Type and size deduced: Ok.

    std::cout << a.size << b.size << c.size;

    return 0;
}

看起來類模板的模板參數推導僅適用于推導所有參數,我期望兩種行為(輔助函數和類模板)是相同的,我是否誤解了什么?

It looks like template argument deduction for class templates works only deducing all the parameters, I was expecting both behaviours (helper function and class template) to be the same, did I misunderstood something?

1Wandbox 中最后可用的編譯器是 gcc HEAD 7.0.1 201701clang HEAD 5.0.0 (trunk).>

1The last compilers availables in Wandbox are gcc HEAD 7.0.1 201701 and clang HEAD 5.0.0 (trunk).

推薦答案

來自這個優(yōu)秀的旅行報告 由 Botond Ballo 撰寫:

From this excellent trip report by Botond Ballo:

最初提議的功能包括部分推導的規(guī)定,您可以在其中明確指定一些模板參數,并將其余部分推導,但由于擔心在某些情況下可能會非?;靵y,因此將其撤消:

The feature as originally proposed included a provision for partial deduction, where you explicitly specify some of the template arguments, and leave the rest to be deduced, but this was pulled over concerns that it can be very confusing in some cases:

// Would have deduced tuple<int, string, float>,
// but tuple<int> is a well-formed type in and of itself!
tuple<int> t(42, "waldo", 2.0f);

這篇關于C++17類模板部分推導的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 99免费在线视频 | 亚洲欧美日韩一区二区 | 国产福利91精品 | 亚洲视频在线播放 | 美女视频一区 | 国产精品久久久久国产a级 欧美日本韩国一区二区 | 日韩精品一二三区 | 伊人网在线看 | 中文字幕日韩在线 | 国产在线视频一区二区 | 99久久免费观看 | 精品一级电影 | 日本特黄a级高清免费大片 成年人黄色小视频 | 亚洲综合第一页 | 亚洲国产看片 | 久久久青草婷婷精品综合日韩 | 久久精品一 | 91最新在线视频 | 欧美成视频 | 波霸ol一区二区 | 亚洲成人一区二区 | 国产精品久久久久久久久久免费看 | 91精品国产综合久久精品 | 久久久91精品国产一区二区精品 | 日韩午夜电影在线观看 | 久久福利网站 | 国产在线a| 九九九久久国产免费 | 激情欧美日韩一区二区 | 中文字幕不卡 | 国产亚洲一区二区三区 | av中文字幕在线 | 一区二区精品视频 | 黄色一级毛片免费看 | 欧洲在线视频 | 国产99在线 | 欧美 | 亚洲精品电影在线观看 | 亚洲一区国产 | 日本一本在线 | 色婷婷激情 | 最新超碰 |