function template specialization question D vs. C++

kdevel kdevel at vogtner.de
Sun Jan 14 00:09:42 UTC 2018


fusp.d
```
import std.stdio;
import std.typecons;

void foo (T) ()
{
    writeln ("(1) foo T = ", T.stringof);
}

void foo (T : float) ()
{
    writeln ("(2) foo T = ", T.stringof);
}

// void foo (T : double) ()
// {
//    writeln ("(2) foo T = ", T.stringof);
// }

void main ()
{
    foo!float;
    foo!double;
    foo!real;
    foo!string;
}
```

prints

    (2) foo T = float
    (2) foo T = double
    (2) foo T = real
    (1) foo T = string

I would have expected

    (2) foo T = float
    (1) foo T = double
    (1) foo T = real
    (1) foo T = string

The compiler does not allow me to specialize the primary function 
template for double:

$ dmd fusp.d
fusp.d(23): Error: fusp.foo called with argument types () matches 
both:
fusp.d(9):     fusp.foo!real.foo()
and:
fusp.d(14):     fusp.foo!real.foo()
fusp.d(23): Error: foo!real has no effect

Is this a compiler error? C++ function template specialization 
works as expected:

fusp.cc
```
#include <iostream>

template<typename T>
void foo ()
{
    std::cout << "(1) "  << __PRETTY_FUNCTION__ << "\n";
}
template<>
void foo<float> ()
{
    std::cout << "(2) " <<  __PRETTY_FUNCTION__ << "\n";
}
template<>
void foo<double> ()
{
    std::cout << "(3) " <<  __PRETTY_FUNCTION__ << "\n";
}

int main ()
{
    foo<int> ();
    foo<float> ();
    foo<double> ();
    foo<long double> ();
    foo<std::string> ();
}
```

$ g++ fusc.cc
$ ./a.out
(1) void foo() [with T = int]
(2) void foo() [with T = float]
(3) void foo() [with T = double]
(1) void foo() [with T = long double]
(1) void foo() [with T = std::__cxx11::basic_string<char>]



More information about the Digitalmars-d-learn mailing list