[cfe-dev] How to perform template substitution in the clang library

stewart mackenzie setori88 at gmail.com
Thu Nov 22 01:06:24 PST 2012


Hi all,

Specifically, we have a C++ source file like this:

    template <int n>
    struct N {};

    struct B {
        template <typename M>
        using A = typename std::conditional<std::is_same<M, N<4>>::value,
                                            int*, void*>::type;
    };

    template <typename T, T value>
    struct F : B {};

    template <>
    struct F<decltype(&fopen), &fopen> : B {
        template <typename M>
        using A = double*;
    };

    template <>
    struct F<decltype(&fclose), &fclose> : B {
        template <typename M>
        using A = typename std::conditional<std::is_same<M, N<16>>::value,
                                            void*, char**>::type;
    };

    // More specialization of 'F' follows.

It is easy to find the
[ClassTemplateDecl](http://clang.llvm.org/doxygen/classclang_1_1ClassTemplateDecl.html)s
of `N` and `F`, and the
[QualType](http://clang.llvm.org/doxygen/classclang_1_1QualType.html)
and [FunctionDecl](http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html)
of the function pointers `&fopen`, `&fclose`, etc. But the problem is
how to substitute these arguments into N, F and F::A without modifying
the source code.

The question is:

* How do I evaluate `F<decltype(&fprintf), &fprintf>::A<N<4>>` and
know that it is an `int*`?
* How do I evaluate `F<decltype(&fopen), &fopen>::A<N<7>>` and know
that it is a `double*`?
* and so on...

Kind regards
Stewart



More information about the cfe-dev mailing list