Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

c++ - boost::enable_if class template method

I got class with template methods that looks at this:

struct undefined {};

template<typename T> struct is_undefined : mpl::false_ {};

template<> struct is_undefined<undefined> : mpl::true_ {};

template<class C>
struct foo {
        template<class F, class V>
        typename boost::disable_if<is_undefined<C> >::type
            apply(const F &f, const V &variables) {
        }

        template<class F, class V>
        typename boost::enable_if<is_undefined<C> >::type
            apply(const F &f, const V &variables) {
        }
};

apparently, both templates are instantiated, resulting in compile time error. is instantiation of template methods different from instantiation of free functions? I have fixed this differently, but I would like to know what is up. the only thing I can think of that might cause this behavior, enabling condition does not depend immediate template arguments, but rather class template arguments

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your C does not participate in deduction for apply. See this answer for a deeper explanation of why your code fails.

You can resolve it like this:

template<class C>
struct foo {    
        template<class F, class V>
        void apply(const F &f, const V &variables) { 
            apply<F, V, C>(f, variables); 
        }

private:
        template<class F, class V, class C1>
        typename boost::disable_if<is_undefined<C1> >::type
            apply(const F &f, const V &variables) {
        }

        template<class F, class V, class C1>
        typename boost::enable_if<is_undefined<C1> >::type
            apply(const F &f, const V &variables) {
        }
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...