[cfe-dev] Specializing a deleted function template?

Marshall Clow mclow.lists at gmail.com
Wed Sep 25 15:00:11 PDT 2013


On Sep 25, 2013, at 4:48 PM, Marshall Clow <mclow.lists at gmail.com> wrote:

> 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;

What I meant here is "if I comment out the "= delete" - not the whole line.

> 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".

gcc 4.7.2 compiles the test program, and gives an error if I uncomment:
> std::cout << ntoh ( 1 ) << std::endl;

junk.cpp: In function ‘int main(int, char**)’:
junk.cpp:25:25: error: use of deleted function ‘constexpr T ntoh(T) [with T = int]’
junk.cpp:14:52: error: declared here

-- 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