Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   prevent template specialization for const? (http://www.go4expert.com/forums/prevent-template-specialization-const-t11090/)

towi 4Jun2008 14:59

prevent template specialization for const?
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

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.

madlex 7Jun2008 18:43

Re: prevent template specialization for const?
This is almoast the same thing if you would have asked this:
I declare this function (sum) somewhere, and I use it like this:

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.

towi 10Jun2008 12:18

Re: prevent template specialization for const?
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.

All times are GMT +5.5. The time now is 02:31.