Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template template argument mismatch

Tags:

c++

The following code

#include <cstdint>

template<typename  T>
struct allo{};

template <typename T,  std::size_t a  = alignof(T)>
struct AA{
    constexpr std::size_t align() { return a; }
};

template<template <typename> typename AT>
struct SAR{
};

using UHR = SAR<allo>;

using AHR = SAR<AA>;


int main()
{
    UHR u;
    AHR a;
    return 0;
}

is accepted by GCC with -std=c++17, but rejected by GCC with std=c++14 and clang (regardless of the C++ dialect).

https://godbolt.org/z/xaE56Y5Pj

Which compiler is right? Is this some change in C++17 that clang hasn't implemented?

like image 353
Bulletmagnet Avatar asked May 22 '26 01:05

Bulletmagnet


1 Answers

Before C++17, template template parameters must use class instead of typename. In C++17, this restriction got lifted and the keywords are equivalent in this context.

Your issue is actually that AA has two template parameters and you try to bind it to AT which as one. C++17 allowed templates with N non-defaulted parameters to act the same as templates with N parameters in total. But clang has not implemented it yet by default, you can use -frelaxed-template-template-args flag as per the answer.

like image 77
Quimby Avatar answered May 27 '26 17:05

Quimby