[cfe-dev] Specializing a deleted function template?

Marshall Clow mclow.lists at gmail.com
Wed Sep 25 14:48:57 PDT 2013


Should this work? (clang doesn't accept it)

> #include <iostream>
> #include <string>
> 
> template <typename T> constexpr T                  ntoh ( T                  net ) noexcept = delete;
> template <>           constexpr unsigned           ntoh ( unsigned           net ) noexcept { return 1; }
> template <>           constexpr unsigned long      ntoh ( unsigned long      net ) noexcept { return 2; }
> template <>           constexpr unsigned long long ntoh ( unsigned long long net ) noexcept { return 3; }
> 
> int main( int argc, char *argv[] )
> {
> // 
> 	std::cout << ntoh ( 1U ) << std::endl;
> 	std::cout << ntoh ( 1UL ) << std::endl;
> 	std::cout << ntoh ( 1ULL ) << std::endl;
> // 	std::cout << ntoh ( 1 ) << std::endl;
> }


If I comment out the line with = delete
> template <typename T> constexpr T                  ntoh ( T                  net ) noexcept; //  = delete;

Then it compiles and runs.
If I uncomment the line
> // 	std::cout << ntoh ( 1 ) << std::endl;

I get a link time error bemoaning the lack of "int ntoh<int>(int)" (which is what I expect).
However, a compile-time error would be better (and that was the point of the = delete on the primary template)
Kyle mentioned core issue 941 as the rationale for this technique.
	http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#941

The clang status page at <http://clang.llvm.org/cxx_dr_status.html> shows the status of this issue as "Unknown".

-- Marshall

Marshall Clow     Idio Software   <mailto:mclow.lists at gmail.com>

A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait).
        -- Yu Suzuki





More information about the cfe-dev mailing list