MetaNN icon indicating copy to clipboard operation
MetaNN copied to clipboard

Can some code from book actually complie?

Open JalanChao opened this issue 3 years ago • 0 comments

#include <iostream>
#include <type_traits>

template < bool AddOrRemoveRef >
struct Fun_;

template <>
struct Fun_< true > {
    template < typename T >
    using type = std::add_lvalue_reference< T >;
};

template <>
struct Fun_< false > {
    template < typename T >
    using type = std::remove_reference< T >;
};

// 注意这里的高级用法
template < typename T >
template < bool AddOrRemoveRef >
using Fun = typename Fun_< AddOrRemoveRef >::template type< T >; //error

// 即使是有别名,还是需要显式说明模板
template < typename T >
using Res_ = Fun< false >;

int main()
{
    Res_< int& >::type h = 3;
    std::cout << h << std::endl;
    return 0;
}

I have tested these code with clang14 and gcc11 and gcc9 all failed. [{ "resource": "/root/code/cpp/vscode/Books/TemplateMetaProgrammingPractice/1.2.1templateAsMetaFucntionInput.cpp", "owner": "generated_diagnostic_collection_name#1", "code": "alias_template_extra_headers", "severity": 8, "message": "Extraneous template parameter list in alias template declaration", "source": "clang", "startLineNumber": 22, "startColumn": 1, "endLineNumber": 22, "endColumn": 6 }]

JalanChao avatar Sep 02 '22 07:09 JalanChao