Functionen als Template Argument.



  • Hallo,

    wärend clang (clang version 3.5.0) folgenden Code problemlos ausführt, sagt g++ (g++ 4.8.1) nein das will ich nicht und beendet die Compiliererei mit der Fehlermeldung:

    error: ‘double(double)’ is not a valid type for a template non-type parameter
    

    http://ideone.com/6kTwCS

    #include <iostream>
    
    double add_one (double x)
    {
    	return x+1;
    }
    
    template <typename Type, Type Instance>
    struct test_type
    {
    	double operator() (double x)
    	{
    		return Instance (x);
    	}
    };
    
    int main ()
    {
    	test_type<decltype(add_one), add_one> test;
    
    	std::cout << test(10.0) << "\n";
    
    	return 0;
    }
    

    Wäre nett wenn mich jemand aufklären könnte warum das so ist und was der Standard dazu sagt.


  • Mod

    Ja, das sollte problemlos kompilieren.

    Folgendes erlaubt den Parameter

    §14.1/8 schrieb:

    A non-type template-parameter of type “array of T” or “function returning T” is adjusted to be of type “pointer to T” or “pointer to function returning T”, respectively.

    und für dein Template-Argument trifft das zu

    §14.3.2 schrieb:

    - a constant expression (5.19) that designates the address of a complete object with static storage duration and external or internal linkage or a function with external or internal linkage [...], expressed (ignoring parentheses) as & id-expression, where the id-expression is the name of an object or function, except that the & may be omitted if the name refers to a function or array[...]

    MfG



  • Also verhält sich der g++ da nicht Standardkonform.

    Das hab ich mir schon gedacht, denn das http://ideone.com/WEXcMI geht ja sowieso.


Anmelden zum Antworten