Can the following funtion be written as a constexpr funtion

closed account (EwCjE3v7)
exercise: Would it be possible to define isShorter as a constexpr? if so, do so. If not. explain why not.

Is it possible, I dont think so

1
2
3
4
bool isShorter(const string &s1, const string &s2)
{
	return s1.size() < s2.size();
}
> Would it be possible to define isShorter as a constexpr?

Yes.

> if so, do so

1
2
3
4
5
6
7
8
9
10
11
12
13
struct string // belongs to the type category literal type
{
    constexpr explicit string( int v = 0 ) : value(v) {} 
    
    constexpr int size() const { return value ; } // size() is constexpr
    
    const int value ;    
};

constexpr bool isShorter( const string& s1, const string& s2 )
{
    return s1.size() < s2.size();
}

http://coliru.stacked-crooked.com/a/220364ec303205ff
closed account (EwCjE3v7)
Havnt done struncts much, so dont understand but Thank you I now know there is a way
YellowPyrmid wrote:
Havnt done struncts much, so dont understand

Have you dabbed in classes yet?
closed account (EwCjE3v7)
No not much, the only thing I have done are a little of structs like a struct that has like 3 types, to calculate mortages etc. Not much
Topic archived. No new replies allowed.