Working on a parser combinator library, which this example is derived from, though obviously some names have been changed to protect the innocent:
#include <string>
#include <stdio.h>
using namespace std;
template <typename T> struct only_string;
template <> struct only_string<string> {};
struct another_type {
explicit operator bool() const { return true; }
};
// only substitute if T is string
template <typename T>
bool operator !(T) {
only_string<T> a;
return true;
}
int main() {
another_type a;
if (!a) {
return 1;
} else {
return 0;
}
}
I've got a template operator ! that should only substitute when T is a string, and another type that has a bool operator on it. If I try to call !a, it finds the operator first, fails to substitute and gives up. Can anyone explain this behavior and how to correct for it?
This is the output with g++ 5.4.0
> g++ -std=c++11 test.cc -o test
test.cc: In instantiation of ‘bool operator!(T) [with T = another_type]’:
test.cc:24:10: required from here
test.cc:17:20: error: ‘only_string<another_type> a’ has incomplete type
only_string<T> a;
^
Aucun commentaire:
Enregistrer un commentaire