简体   繁体   中英

Template Argument Deduction Broken in Clang 6 for Temporary Objects

Template argument deduction appears to be broken in Clang 6 for temporary objects.

g++ 8.1.0 compiles and runs the example correctly.

Clang 6.0.0 and 6.0.2 both error at the indicated line with this message:

error: expected unqualified-id
    Print{1,"foo"s,2};  /********** Broken in Clang **********/

All Other lines work correctly.

The behavior is the same in both cases whether -std=c++17 or -std=c++2a is used.

The Clang c++ Status Page indicates that template argument deduction was implemented as of Clang 5 (P0091R3, P0512R0).

Is this a bug? Are there workarounds (eg compiler flags, not code changes )?

example:

template<class ...Ts>
void print(Ts...ts){ (( cout << ... << ts )); }
template<class ...Ts>
struct Print {
    Print(Ts...ts){ (( cout << ... << ts )); }
};

int main(){
    Print{1,"foo"s,2}; /********** Broken in Clang **********/
    Print<int,string,int>{1,"foo"s,2};
    auto p1 = Print{1,"foo"s,2};
    Print p2{1,"foo"s,2};
    print(1,"foo"s,2);
}

This is Clang bug 34091 .

Luckily, it is already fixed, and the trunk build of Clang compiles this without issue .

As far as I know, however, there is currently no way to work around this without code changes, short of upgrading to the next Clang release whenever that comes out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM