Indiana Unversity logo[ConceptGCC]

ConceptGCC :

Re: Requiring a function template specialization

From: Tom Honermann (tom.honermann_at_[hidden])
Date: 2007-06-06 20:52:26


I'm not sure if that is sufficient. Consider this code:

    #include <iostream>

    template<typename T>
    T foo() {
        return 0;
    }

    template<>
    int foo<int>() {
        return 1;
    }

    int foo() {
        return 2;
    }

    int main() {
        std::cout << "foo<char>(): " << (int)foo<char>() << std::endl;
    // writes 0
        std::cout << "foo<int>(): " << foo<int>() << std::endl;
    // writes 1
        std::cout << "foo(): " << foo() << std::endl;
    // writes 2
        // With out the non-template 'int foo()' above, this last line
    won't compile
        // (error: no matching function for call to `foo()')
        return 0;
    }

Compiled with conceptgcc-boostcon (g++, not conceptg++) on Linux, this
produces

    foo<char>(): 0
    foo<int>(): 1
    foo(): 2

It is conceivable that a concept may require both the template
specialization and non-template functions to exist. In this case, the
caller needs to make the call explicit - which would require template
syntax. Alternatively, a concept_map could potentially handle this, but
that doesn't feel right to me - how would it know whether the call
should be satisfied by a template or non-template function?

Tom.

Douglas Gregor wrote:

>On Jun 6, 2007, at 4:36 PM, Walter E Brown wrote:
>
>
>
>>I would like to express a concept, Foo<T>, that requires the existence
>>of a specialization, foo<T>, of a function template.
>>
>>I tried the following with conceptgcc-boostcon under Cygwin, but
>>got an
>>ICE (I submitted a bug report earlier today):
>>
>> #include <concepts>
>>
>> concept Foo< class T >
>> {
>> template<>
>> T
>> foo<T>();
>> };
>>
>>Is the above the correct way to express the desired concept? Thanks,
>>
>>
>
>A template specialization has to specialize something. In this case,
>there is no primary template to be specialized. One thing you can do
>is write this:
>
> concept Foo<class T> {
> T foo();
> }
>
>You can call it as just "foo()", or (to be more specific), Foo<T>::foo
>().
>
> - Doug
>_______________________________________________
>ConceptGCC mailing list
>ConceptGCC_at_[hidden]
>http://www.osl.iu.edu/mailman/listinfo.cgi/conceptgcc
>
>

-- 
Oracle Email Signature Logo
Tom Honermann | Senior Principal Software Engineer | 503.276.2354
Oracle PeopleTools Development
1211 SW 5th Ave, Suite 9080, Portland, OR 97204



oracle_sig_logo.gif