我已经实现了一个类模板,该类模板负责构造单个类型(遵循构建器模式)。构建器的构造函数用于推断两种类型。
下面是一个演示问题的示例(使用编译器资源管理器)。我在-std = c ++ 17上使用了clang 6。
TOC = []
GOF = []
with open('file.txt') as f:
for line in f:
line_list = line.rstrip('\n').split(',')
if line.startswith("TOC"):
TOC.append(line_list[3])
if line.startswith("GOF"):
GOF.append(line_list[3])
这是我收到的错误消息。
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="{% url 'home' %}">AppName</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
{% if user.is_authenticated %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ user.email }}
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{% url 'edit_Profile' %}">Edit Profile</a>
<a class="dropdown-item" href="{% url 'change_password' %}">change Password</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="{% url 'logout' %}">Log Out</a>
</div>
</li>
{% if user.profile.avatar %}
<!img src=" {{ user.profile.avatar.url }}" width="200" height="40" alt="">
{% endif %}
{% else %}
<li class="nav-item">
<a class="nav-link" href="{% url 'login' %}">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'register' %}">Register</a>
</li>
{% endif %}
</ul>
</div>
</nav>
我已经通过以下方式解决了这个问题:
#include <utility>
template <typename T>
struct builder
{
explicit builder(T& _t);
auto option(int x) -> builder&;
auto build() -> int;
};
template <typename T>
void build_it(T& _t, int _u)
{
// Why does the line below not compile?
// C++17 compilers should be able to deduce the type, right?
auto obj = builder{_t}.option(_u).build();
}
)
x86-64 clang 6.0.0 (Editor #1, Compiler #1) C++
x86-64 clang 6.0.0
-std=c++17 -O2 -Wall -Wextra
1
<Compilation failed>
x86-64 clang 6.0.0 - 455ms
#1 with x86-64 clang 6.0.0
<source>:15:27: error: member reference base type 'builder' is not a structure or union
auto obj = builder{_t}.option(_u).build();
~~~~~~~~~~~^~~~~~~~~
1 error generated.
Compiler returned: 1
)make_builder(...)
)我仍然想知道编译器会发生什么变化? 编译器无法推断出类型吗? C ++ 17支持这一点,对吧?