There is a way to force a string literal argument: make a user defined literal operator. You can make the operator constexpr to get the size at compile time: constexpr Literal operator "" _suffix(char const* str, size_t len); return Literal(chars, len); } I don't know of any compiler that implements this feature at this time.
1 for the genius... and insane :) – Matthieu M. Sep 30 at 17:17 This operator does not allow for " though :( – Johannes Schaub - litb Sep 30 at 17:33 @JohannesSchaub-litb oh :( You're right. It is only for integer and floating-point literals.
That's a pity. I'm leaving the answer, but it's not as cool now :( – R. Martinho Fernandes Sep 30 at 17:43 1 @deft_code UD literal operators are allowed to be constexpr.
Here's how good the GCC support of constexpr is (this compiles fine with a snapshot of 4.7), so your problem is essentially solved as soon as UD literals are supported. – Luc Danton Sep 30 at 23:47 2 to experiment further, here is @Luc's code without macros (which is then not a simulation of the UD literal function anymore though): ideone. Com/JF4cx – Johannes Schaub - litb Sep 307 at 0:07.
Yes. You can generate compile time error with following preprocessor: #define IS_STRING_LITERAL(X) "" X "" If you try to pass anything other than a string literal, the compilation will fail. Usage: Literal greet(IS_STRING_LITERAL("")); // ok Literal greet(IS_STRING_LITERAL(broke)); // error.
3 This solution depends on everyone being a good citizen when the entire point of the question is to stop people from being a bad citizen. – JaredPar Sep 30 at 17:59 IS_STRING_LITERAL(+ 0). – Johannes Schaub - litb Sep 30 at 18:43 @JohannesSchaub-litb, actually, I intentionally put "" X format (which generates better compiler error).
I think X "" resolves that issue. Edited – iammilind Sep 30 at 18:45 IS_STRING_LITERAL(0 +) – Johannes Schaub - litb Sep 30 at 18:45 @JohannesSchaub-litb, how about this – iammilind Sep 30 at 18:52.
No there is no way to do this. String literals have a particular type and all method overload resolution is done on that type, not that it's a string literal. Any method which accepts a string literal will end up accepting any value which has the same type.
If your function absolutely depends on an item being a string literal to function then you probably need to revisit the function. It's depending on data it can't guarantee.
No there is no way to do this. Of course it's possible. – iammilind Sep 30 at 17:51 3 @iammilind your method depends on every caller being a good citizen.
The entire purpose of the question is to stop people from being bad citizens. – JaredPar Sep 30 at 17:57.
A string literal does not have a separate type to distinguish it from a const char array. This, however, will make it slightly harder to accidentally pass (non-const) char arrays. #include struct Literal { template Literal( const char (&literal)N ){} template Literal( char (&literal)N ) = delete; }; int main() { Literal greet( "" ); char a = " Literal broke(a); //fails } As to runtime checking, the only problem with a non-literal is that it may not be null-terminated?
As you know the size of the array, you can loop over it (preferable backwards) to see if there's a \0 in it.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.