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

stewart mackenzie setori88 at gmail.com
Mon Nov 26 14:55:48 PST 2012


Hi all,

I have it on authority that the timing of my previous questions were
dismal! So I'll start the question on a happy Thanksgiving note.

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 ClassTemplateDecls (
http://clang.llvm.org/doxygen/classclang_1_1ClassTemplateDecl.html) 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20121127/fe99116e/attachment.html>


More information about the cfe-dev mailing list