czs*_*108 5 c++ template-argument-deduction c++17 deduction-guide
#include <variant>
struct A { };
struct B { };
struct Test
{
template<typename Ts>
struct overloaded : Ts { };
// 1st Deduction Guide
template<typename Ts>
overloaded(Ts)->overloaded<Ts>;
// 2nd Deduction Guide for class "A"
overloaded(A)->overloaded<A>;
void Fun()
{
// OK
overloaded obj1{ A{} };
// Error: No instance of constructor matches the argument list
// I think the 1st Deduction Guide is not effective inside class "Test"
overloaded obj2{ B{} };
}
};
int main()
{
// All Deduction Guides in class "Test" are effective here
// OK
Test::overloaded obj1{ A{} };
// OK
Test::overloaded obj2{ B{} };
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我C++17在一个名为Test.
但是我发现包含模板参数的 1st Deduction GuideTs在类Test本身内部无效。但在它之外有效,例如在main功能上。
template<typename Ts>
overloaded(Ts)->overloaded<Ts>;
Run Code Online (Sandbox Code Playgroud)
不包含模板参数的第二推导指南在课堂内外都有效。
overloaded(A)->overloaded<A>;
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么?
我的 IDE 是Visual Studio 2019,C++ 标准是C++17.
谢谢你。
| 归档时间: |
|
| 查看次数: |
130 次 |
| 最近记录: |