Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Bert_Jonson
Beginner
80 Views

constexpr strange behavior on strings

I've found that ICC works strange on string when function is constexpr.

[cpp]

#include <tuple>

using namespace std;

constexpr int str_len(const char *s) {
return *s ? 1 + str_len(s + 1) : 0;
}

int main() {
constexpr int i = str_len("");

tuple<int, int> t;
get<i>(t) = 1; /// let's set first element of tuple to 1
}

[/cpp]

error: expression must have a constant value
get<i>(t) = 1; /// let's set first element of tuple to 1

If we change str_len to this:

[cpp]

constexpr int str_len(const char *s) {
return 0;
}

[/cpp]

Then the all is ok. It seems that ICC doesn't support actions on strings in compile time at all. Will it be fixed? GCC correctly works on this.

ICC 13 update 1 on win.

0 Kudos
5 Replies
Georg_Z_Intel
Employee
80 Views

Hello, thank you for reporting this. I've forwarded the problem description to engineering (DPD200240624) and let you know once fixed. Edit: As documented we currently support constexpr only partially -- please see here: http://software.intel.com/en-us/articles/c0x-features-supported-by-intel-c-compiler Best regards, Georg Zitzlsberger
SergeyKostrov
Valued Contributor II
80 Views

Hi everybody, I did a very quick test and, unfortunately, I couldn't even compile the test case. It fails on: ... get( t ) = 1; ... I'll try to spend more time later. However, the str_len function is a classic example of a recursion ( I did small modifications ): ... constexpr int str_len( const char *s ) { return ( constexpr & )( *s != 0x0 ) ? ( 1 + str_len( s + 1 ) ) : 0; } ... and because of this it looks like Intel C++ compiler doesn't assume that expression ( 1 + str_len( s + 1 ) is a constant. If s = "12345" it will call itself 5 times and it will return 5 values, that is 1, 2, 3, 4 and 5 ( 5 is the length of the input string ).
Bert_Jonson
Beginner
80 Views

Hi man.

But function result is constant and known at compile time.

So it's compiler mistake.

SergeyKostrov
Valued Contributor II
80 Views

>>... >>But function result is constant and known at compile time. >> >>So it's compiler mistake. Nobody argues here about the problem and Georg already confirmed it. Thanks for your time.
Georg_Z_Intel
Employee
80 Views

Hello, the next minor version (Composer XE 2013 SP1) will have (full) support for that. This version is currently in BETA phase and will be available by end of this year (2013). Thank you for bringing this up! Best regards, Georg Zitzlsberger
Reply