On Thursday, 13 June 2013 at 10:52:56 UTC, TommiT wrote:
Why is the following ambiguous?
-----------
module a;
int foo(T)() if (is(T == int))
{
return 42;
}
-----------
module b;
int foo(T)() if (is(T == char))
{
return 3;
}
-----------
module main;
import std.stdio;
import a;
import b;
void main()
{
writeln(foo!int()); // Error: ambiguous template declaration
}
The corresponding code in C++ works nicely:
-------------------------------------------
// a.h
#include <type_traits>
template <typename T, typename std::enable_if<
std::is_same<T, int>::value,
int>::type = 0>
int foo()
{
return 42;
}
-------------------
// b.h
#include <type_traits>
template <typename T, typename std::enable_if<
std::is_same<T, char>::value,
int>::type = 0>
int foo()
{
return 3;
}
-------------------
// main.cpp
#include <iostream>
#include "a.h"
#include "b.h"
int main()
{
std::cout << foo<int>() << std::endl; // 42
std::cout << foo<char>() << std::endl; // 3
return 0;
}
I'm just wondering whether the fact that the above (quoted) code
doesn't work in D is based on some fundamental difference in how
D and C++ work, or if it's some kind of a defect in D which we
could change and make the above D code work.