lundi 28 mars 2016

Error with SFINAE

Why does the following code cause an error? I would think the compiler just picks the appropriate overload here?

#include <iostream>
using std::cout;
using std::endl;

template <typename ToCheckFor>
struct InterfaceCheck {

    // used by the constexpr function, the function will pass in a pointer to
    // a type with the required types
    template <typename _ToCheckFor, void (_ToCheckFor::*) ()>
    struct InterfaceCheckImplTag {};

    // used to check for the presence of a function print()
    // template <typename T>
    // static constexpr bool function(__attribute__((unused)) void* ptr) {}
    template <typename T>
    static constexpr bool function(__attribute__((unused)) void* ptr) { 
        return false; 
    }
    template <typename T>
    static constexpr bool function (__attribute__((unused))  
         InterfaceCheckImplTag<T, &T::print>* ptr) { 

        return true; 
    }

    constexpr static const bool value = function<ToCheckFor>(nullptr);
};

struct Something {
    void print() { cout << "Something::print()" << endl; }
};

int main() {

    cout << InterfaceCheck<Something>::value << endl;

    return 0;
}

Why does replacing the void* argument with an ellipsis make the code work as expected?

Aucun commentaire:

Enregistrer un commentaire