Ambiguity error in C++17 (template template parameters and default arguments issue)

前端 未结 1 1875
栀梦
栀梦 2021-02-12 04:04

I have code which is differently interpreted by g++ with the c++14 and c++17 standard flags:

#include 
#include 

        
相关标签:
1条回答
  • 2021-02-12 04:18

    The behavior changed since C++17.

    Before C++17, the code works because std::vector has two template parameters (the 2nd one has the default argument std::allocator<T>), while the template template parameter Vector is declared to have only one, they don't match then the 2nd func won't be considered.

    Since C++17 (CWG 150), the default template arguments are allowed for a template template argument to match a template template parameter with fewer template parameters. That means both func become valid candidates and then leads to ambiguity.

    template<class T> class A { /* ... */ };
    template<class T, class U = T> class B { /* ... */ };
    
    template<template<class> class P> class X { /* ... */ };
    
    X<A> xa; // OK
    X<B> xb; // OK in C++17 after CWG 150
             // Error earlier: not an exact match
    
    0 讨论(0)
提交回复
热议问题