Kory
Kory

Reputation: 133

Why does the compiler require the type to be specified?

I've implemented a class template that is responsible for constructing a single type (follows the builder pattern). The builder's constructor is used to deduce two types.

Below is an example that demonstrates the problem (using compiler explorer). I used clang 6 with -std=c++17.

#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();
}

And here is the error message I receive.


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

I've solved this in the following ways:

I'd still like to know what the compiler is upset about? Can't the compiler deduce the type? C++17 supports this, right?

Upvotes: 3

Views: 126

Answers (1)

Barry
Barry

Reputation: 302932

This is clang bug 41450. The program is valid.

Upvotes: 7

Related Questions