模板模板参数不可见是否有解决方法?

Is there a workaround for template template arguments being invisible?

考虑以下代码:

template<typename... Args1> 
constexpr size_t direct = sizeof... (Args1);

template<template<typename... Args1> typename A1> 
constexpr size_t nested = sizeof... (Args1); // compile error ☹️

嵌套行无法编译,因为(我推测)Args1 是一个嵌套参数。

有没有办法以非侵入式的方式解决这个问题?通过侵入我的意思是修改传递的类型,例如添加

constexpr size_t sizeof_args = sizeof... (Args...);

传递给此模板的每个模板。

注意:我可以使用 C++20 概念,但据我所知,他们没有提供这方面的改进 space。

可以写一个初级变量模板,然后将变量模板特化为模板模板参数。

这允许您单独显式命名模板模板参数的模板参数:

template<typename ...> 
constexpr size_t nested = -1; // any value will do, 
                              // but prefer one that is invalid for specializations

template<template<typename ...> typename A1, typename... Args1> 
                // name the parameters here  ^_______________^
constexpr size_t nested<A1<Args1...>> = sizeof...(Args1);
    // specialize here ^____________^

C++20 中添加的概念功能在这里没有任何帮助,因为没有任何限制。

这是一个demo