简体   繁体   中英

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

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) {
        }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM