Q
Qi
Let me show some test code.
=====code start
template <typename T>
void abc(const T &)
{
cout << "1" << endl;
}
template <typename T>
void test(const T & a)
{
abc(a);
}
void abc(const string &)
{
cout << "2" << endl;
}
int main(int argc, char * argv[])
{
(void)argc;
(void)argv;
string s;
test(s);
return 0;
}
=====code end
"abc" is a template function that I use as some kind of traits.
(similar as get_pointer in Boost).
The "string" overloading is after the "test" function.
"test" is the function where using "abc". "test" is a template
function too.
"main" is the function that using "test". It's guaranteed that
all overloading will be declared before main (but may after "test").
In standard way, above code will print "1" on GCC (VC will print "2"
because of its less standard template parsing).
My question is, is there any way to make "test" chooses
the "string" overloading and print 2, no matter the overloading
is before or after "test"?
The reason I want that is, the overloading functions may spread
in multiple headers, if "test" can only choose the overloading
that declared before it, that will enforce the including order of
the headers.
Thanks
=====code start
template <typename T>
void abc(const T &)
{
cout << "1" << endl;
}
template <typename T>
void test(const T & a)
{
abc(a);
}
void abc(const string &)
{
cout << "2" << endl;
}
int main(int argc, char * argv[])
{
(void)argc;
(void)argv;
string s;
test(s);
return 0;
}
=====code end
"abc" is a template function that I use as some kind of traits.
(similar as get_pointer in Boost).
The "string" overloading is after the "test" function.
"test" is the function where using "abc". "test" is a template
function too.
"main" is the function that using "test". It's guaranteed that
all overloading will be declared before main (but may after "test").
In standard way, above code will print "1" on GCC (VC will print "2"
because of its less standard template parsing).
My question is, is there any way to make "test" chooses
the "string" overloading and print 2, no matter the overloading
is before or after "test"?
The reason I want that is, the overloading functions may spread
in multiple headers, if "test" can only choose the overloading
that declared before it, that will enforce the including order of
the headers.
Thanks