prevent template specialization for const?

Discussion in 'C' started by towi, Jun 4, 2008.

  1. towi

    towi New Member

    Joined:
    Jun 4, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I try to do a bit of templating, but there is a problem.

    /// base template. no implementation, just declaration.
    template<typename ANY> void func(ANY&);

    /// specialization for int
    template<> void func<int>(int &val) { val = 7; }

    /// specialization for string
    template<> void func<string>(string&val) { val="hello"; }


    but: when I write somewhere in my code

    main(...)
    {
    string s1;
    func(s1); // fine

    const string s2;
    func(s2); // compiles, but link error.
    }


    What happens is clear: I only specialized the non-const-ref version for "string", but I would need the const-ref here. So the compiler falls back on the unspecialized variant here, where I did not provide an implementation for. This of course the compiler can not detect, because I could provide an implementation anywhere, if I liked. But the linker will fail, because I dont provide one -- because the way I use "func()" a const-ref would not make sense.


    I wonder, is there some mechanism which I could use to let the compiler warn me when I use "func" on a "const string"?

    Thanks in advance.

    tschau, towi.
     
  2. madlex

    madlex New Member

    Joined:
    Jun 6, 2008
    Messages:
    12
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Technical Project Manager
    Location:
    Bucharest,RO
    This is almoast the same thing if you would have asked this:
    I declare this function (sum) somewhere, and I use it like this:
    Code:
    int sum(int a,int b);
    int main(void){ sum(1,2); }
    but I don't implement it anywhere, how could I get a compiler warning?

    Of course, you won't. The compiler will compile every cpp based on the declarations it founds mostly in files like .h. If everything seems to be in order syntactic and semantic, it will throw no warning or errors. Is the liker's job to assemble all the implementations together and bind the related links.

    In case of template functions, it is a little bit more complicated, but respects the same principle. Basicly the compiler compiles twice the template functions. First it check the generic implementation for syntactic errors, and then, when a particular instantiation is done, it checks again for semantic errors just like in any other function.
    In your case doesn't apply, because you haven't implemented, just like you said, neither the generic template, neither a specialization for string const &. So as I said above, this is a linker's job only.
    To get a compiler warning, you need to implement the template function template<typename ANY> void func(ANY&);, because you are tricking the compiler that this function exists, when...it doesn't.
    Cheers
     
    shabbir likes this.
  3. towi

    towi New Member

    Joined:
    Jun 4, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the explanation.

    Yes, what the compiler/linker does is completely clear to me. I think my question should have been "Is there a different approach which I could take to prevent expressions in 'const string&' go through the compiler?"
    Maybe with type-traits, or by declaring the template only to apply for non-consts?
    I am out of ideas there...

    > you need to implement the template function template<typename ANY> void func(ANY&)

    Hm, maybe I can see what you mean... if I'll write

    template<typename ANY> void func(ANY& val)
    {
    val = val;
    }

    I would get a compiler error when val ist const&. How silly of me... I will try that out immediatly!

    tschau, towi.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice