std::string like compile-time const char* string












4












$begingroup$


I want to write a very simple std::string like compile-time const char* string.



I need to work with strings in the compiletime, just like with strings, I implemented basic functions.



class cstring final {
const char* str_;
std::size_t size_;

public:
constexpr cstring(const char* str, std::size_t size, std::size_t prefix = 0, std::size_t suffix = 0) noexcept
: str_{str + prefix},
size_{size - prefix - suffix} {}

template <std::size_t N>
constexpr cstring(const char (&str)[N]) noexcept : cstring{str, N - 1, 0, 0} {}

constexpr cstring() noexcept : cstring{nullptr, 0, 0, 0} {}

cstring(const std::string& str) noexcept : cstring{str.data(), str.size(), 0, 0} {}

constexpr cstring(const cstring&) = default;

cstring& operator=(const cstring&) = default;

constexpr std::size_t size() const noexcept { return size_; }

constexpr std::size_t length() const noexcept { return size_; }

constexpr std::size_t max_size() const noexcept {
return (std::numeric_limits<std::size_t>::max)();
}

constexpr bool empty() const noexcept { return size_ == 0; }

constexpr const char* begin() const noexcept { return str_; }

constexpr const char* end() const noexcept { return str_ + size_; }

constexpr const char* cbegin() const noexcept { return begin(); }

constexpr const char* cend() const noexcept { return end(); }

constexpr const char& operator(std::size_t i) const { return str_[i]; }

constexpr const char& at(std::size_t i) const {
return (i < size_) ? str_[i]
: (throw std::out_of_range{"cstring::at"}, str_[0]);
}

constexpr const char& front() const { return str_[0]; }

constexpr const char& back() const { return str_[size_ - 1]; }

constexpr const char* data() const noexcept { return str_; }

constexpr cstring remove_prefix(std::size_t n) const {
return {str_ + n, size_ - n};
}

constexpr cstring add_prefix(std::size_t n) const {
return {str_ - n, size_ + n};
}

constexpr cstring remove_suffix(std::size_t n) const {
return {str_, size_ - n};
}

constexpr cstring add_suffix(std::size_t n) const {
return {str_, size_ + n};
}

constexpr cstring substr(std::size_t pos, std::size_t n) const {
return {str_ + pos, n};
}

constexpr int compare(cstring other) const {
return (size_ == other.size_) ? detail::StrCompare(str_, other.str_, size_)
: ((size_ > other.size_) ? 1 : -1);
}

friend constexpr bool operator==(cstring lhs, cstring rhs) {
return lhs.compare(rhs) == 0;
}

friend constexpr bool operator!=(cstring lhs, cstring rhs) {
return !(lhs == rhs);
}

std::string append(cstring s) const {
return std::string{str_, size_}.append(s.str_, s.size_);
}

friend std::string operator+(cstring lhs, cstring rhs) {
return std::string{lhs.str_, lhs.size_} + std::string{rhs.str_, rhs.size_};
}

friend std::ostream& operator<<(std::ostream& os, cstring str) {
os.write(str.str_, str.size_);
return os;
}

operator std::string() const { return std::string{str_, size_}; }
};









share|improve this question







New contributor




Neargye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 2




    $begingroup$
    Welcome to Code Review. Could you add some additional information to your code, e.g. if you tried to reproduce the string_view interface, what you want reviewers to focus on, and so on? Also, you want to add missing #includes for std::string and std::numeric_limits. A usage example of your class would also be much appreciated.
    $endgroup$
    – Zeta
    15 hours ago










  • $begingroup$
    Some indentation would be nice.
    $endgroup$
    – Quuxplusone
    14 hours ago










  • $begingroup$
    Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing.
    $endgroup$
    – Neargye
    13 hours ago
















4












$begingroup$


I want to write a very simple std::string like compile-time const char* string.



I need to work with strings in the compiletime, just like with strings, I implemented basic functions.



class cstring final {
const char* str_;
std::size_t size_;

public:
constexpr cstring(const char* str, std::size_t size, std::size_t prefix = 0, std::size_t suffix = 0) noexcept
: str_{str + prefix},
size_{size - prefix - suffix} {}

template <std::size_t N>
constexpr cstring(const char (&str)[N]) noexcept : cstring{str, N - 1, 0, 0} {}

constexpr cstring() noexcept : cstring{nullptr, 0, 0, 0} {}

cstring(const std::string& str) noexcept : cstring{str.data(), str.size(), 0, 0} {}

constexpr cstring(const cstring&) = default;

cstring& operator=(const cstring&) = default;

constexpr std::size_t size() const noexcept { return size_; }

constexpr std::size_t length() const noexcept { return size_; }

constexpr std::size_t max_size() const noexcept {
return (std::numeric_limits<std::size_t>::max)();
}

constexpr bool empty() const noexcept { return size_ == 0; }

constexpr const char* begin() const noexcept { return str_; }

constexpr const char* end() const noexcept { return str_ + size_; }

constexpr const char* cbegin() const noexcept { return begin(); }

constexpr const char* cend() const noexcept { return end(); }

constexpr const char& operator(std::size_t i) const { return str_[i]; }

constexpr const char& at(std::size_t i) const {
return (i < size_) ? str_[i]
: (throw std::out_of_range{"cstring::at"}, str_[0]);
}

constexpr const char& front() const { return str_[0]; }

constexpr const char& back() const { return str_[size_ - 1]; }

constexpr const char* data() const noexcept { return str_; }

constexpr cstring remove_prefix(std::size_t n) const {
return {str_ + n, size_ - n};
}

constexpr cstring add_prefix(std::size_t n) const {
return {str_ - n, size_ + n};
}

constexpr cstring remove_suffix(std::size_t n) const {
return {str_, size_ - n};
}

constexpr cstring add_suffix(std::size_t n) const {
return {str_, size_ + n};
}

constexpr cstring substr(std::size_t pos, std::size_t n) const {
return {str_ + pos, n};
}

constexpr int compare(cstring other) const {
return (size_ == other.size_) ? detail::StrCompare(str_, other.str_, size_)
: ((size_ > other.size_) ? 1 : -1);
}

friend constexpr bool operator==(cstring lhs, cstring rhs) {
return lhs.compare(rhs) == 0;
}

friend constexpr bool operator!=(cstring lhs, cstring rhs) {
return !(lhs == rhs);
}

std::string append(cstring s) const {
return std::string{str_, size_}.append(s.str_, s.size_);
}

friend std::string operator+(cstring lhs, cstring rhs) {
return std::string{lhs.str_, lhs.size_} + std::string{rhs.str_, rhs.size_};
}

friend std::ostream& operator<<(std::ostream& os, cstring str) {
os.write(str.str_, str.size_);
return os;
}

operator std::string() const { return std::string{str_, size_}; }
};









share|improve this question







New contributor




Neargye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 2




    $begingroup$
    Welcome to Code Review. Could you add some additional information to your code, e.g. if you tried to reproduce the string_view interface, what you want reviewers to focus on, and so on? Also, you want to add missing #includes for std::string and std::numeric_limits. A usage example of your class would also be much appreciated.
    $endgroup$
    – Zeta
    15 hours ago










  • $begingroup$
    Some indentation would be nice.
    $endgroup$
    – Quuxplusone
    14 hours ago










  • $begingroup$
    Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing.
    $endgroup$
    – Neargye
    13 hours ago














4












4








4





$begingroup$


I want to write a very simple std::string like compile-time const char* string.



I need to work with strings in the compiletime, just like with strings, I implemented basic functions.



class cstring final {
const char* str_;
std::size_t size_;

public:
constexpr cstring(const char* str, std::size_t size, std::size_t prefix = 0, std::size_t suffix = 0) noexcept
: str_{str + prefix},
size_{size - prefix - suffix} {}

template <std::size_t N>
constexpr cstring(const char (&str)[N]) noexcept : cstring{str, N - 1, 0, 0} {}

constexpr cstring() noexcept : cstring{nullptr, 0, 0, 0} {}

cstring(const std::string& str) noexcept : cstring{str.data(), str.size(), 0, 0} {}

constexpr cstring(const cstring&) = default;

cstring& operator=(const cstring&) = default;

constexpr std::size_t size() const noexcept { return size_; }

constexpr std::size_t length() const noexcept { return size_; }

constexpr std::size_t max_size() const noexcept {
return (std::numeric_limits<std::size_t>::max)();
}

constexpr bool empty() const noexcept { return size_ == 0; }

constexpr const char* begin() const noexcept { return str_; }

constexpr const char* end() const noexcept { return str_ + size_; }

constexpr const char* cbegin() const noexcept { return begin(); }

constexpr const char* cend() const noexcept { return end(); }

constexpr const char& operator(std::size_t i) const { return str_[i]; }

constexpr const char& at(std::size_t i) const {
return (i < size_) ? str_[i]
: (throw std::out_of_range{"cstring::at"}, str_[0]);
}

constexpr const char& front() const { return str_[0]; }

constexpr const char& back() const { return str_[size_ - 1]; }

constexpr const char* data() const noexcept { return str_; }

constexpr cstring remove_prefix(std::size_t n) const {
return {str_ + n, size_ - n};
}

constexpr cstring add_prefix(std::size_t n) const {
return {str_ - n, size_ + n};
}

constexpr cstring remove_suffix(std::size_t n) const {
return {str_, size_ - n};
}

constexpr cstring add_suffix(std::size_t n) const {
return {str_, size_ + n};
}

constexpr cstring substr(std::size_t pos, std::size_t n) const {
return {str_ + pos, n};
}

constexpr int compare(cstring other) const {
return (size_ == other.size_) ? detail::StrCompare(str_, other.str_, size_)
: ((size_ > other.size_) ? 1 : -1);
}

friend constexpr bool operator==(cstring lhs, cstring rhs) {
return lhs.compare(rhs) == 0;
}

friend constexpr bool operator!=(cstring lhs, cstring rhs) {
return !(lhs == rhs);
}

std::string append(cstring s) const {
return std::string{str_, size_}.append(s.str_, s.size_);
}

friend std::string operator+(cstring lhs, cstring rhs) {
return std::string{lhs.str_, lhs.size_} + std::string{rhs.str_, rhs.size_};
}

friend std::ostream& operator<<(std::ostream& os, cstring str) {
os.write(str.str_, str.size_);
return os;
}

operator std::string() const { return std::string{str_, size_}; }
};









share|improve this question







New contributor




Neargye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I want to write a very simple std::string like compile-time const char* string.



I need to work with strings in the compiletime, just like with strings, I implemented basic functions.



class cstring final {
const char* str_;
std::size_t size_;

public:
constexpr cstring(const char* str, std::size_t size, std::size_t prefix = 0, std::size_t suffix = 0) noexcept
: str_{str + prefix},
size_{size - prefix - suffix} {}

template <std::size_t N>
constexpr cstring(const char (&str)[N]) noexcept : cstring{str, N - 1, 0, 0} {}

constexpr cstring() noexcept : cstring{nullptr, 0, 0, 0} {}

cstring(const std::string& str) noexcept : cstring{str.data(), str.size(), 0, 0} {}

constexpr cstring(const cstring&) = default;

cstring& operator=(const cstring&) = default;

constexpr std::size_t size() const noexcept { return size_; }

constexpr std::size_t length() const noexcept { return size_; }

constexpr std::size_t max_size() const noexcept {
return (std::numeric_limits<std::size_t>::max)();
}

constexpr bool empty() const noexcept { return size_ == 0; }

constexpr const char* begin() const noexcept { return str_; }

constexpr const char* end() const noexcept { return str_ + size_; }

constexpr const char* cbegin() const noexcept { return begin(); }

constexpr const char* cend() const noexcept { return end(); }

constexpr const char& operator(std::size_t i) const { return str_[i]; }

constexpr const char& at(std::size_t i) const {
return (i < size_) ? str_[i]
: (throw std::out_of_range{"cstring::at"}, str_[0]);
}

constexpr const char& front() const { return str_[0]; }

constexpr const char& back() const { return str_[size_ - 1]; }

constexpr const char* data() const noexcept { return str_; }

constexpr cstring remove_prefix(std::size_t n) const {
return {str_ + n, size_ - n};
}

constexpr cstring add_prefix(std::size_t n) const {
return {str_ - n, size_ + n};
}

constexpr cstring remove_suffix(std::size_t n) const {
return {str_, size_ - n};
}

constexpr cstring add_suffix(std::size_t n) const {
return {str_, size_ + n};
}

constexpr cstring substr(std::size_t pos, std::size_t n) const {
return {str_ + pos, n};
}

constexpr int compare(cstring other) const {
return (size_ == other.size_) ? detail::StrCompare(str_, other.str_, size_)
: ((size_ > other.size_) ? 1 : -1);
}

friend constexpr bool operator==(cstring lhs, cstring rhs) {
return lhs.compare(rhs) == 0;
}

friend constexpr bool operator!=(cstring lhs, cstring rhs) {
return !(lhs == rhs);
}

std::string append(cstring s) const {
return std::string{str_, size_}.append(s.str_, s.size_);
}

friend std::string operator+(cstring lhs, cstring rhs) {
return std::string{lhs.str_, lhs.size_} + std::string{rhs.str_, rhs.size_};
}

friend std::ostream& operator<<(std::ostream& os, cstring str) {
os.write(str.str_, str.size_);
return os;
}

operator std::string() const { return std::string{str_, size_}; }
};






c++ c++11






share|improve this question







New contributor




Neargye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Neargye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Neargye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 16 hours ago









NeargyeNeargye

211




211




New contributor




Neargye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Neargye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Neargye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2




    $begingroup$
    Welcome to Code Review. Could you add some additional information to your code, e.g. if you tried to reproduce the string_view interface, what you want reviewers to focus on, and so on? Also, you want to add missing #includes for std::string and std::numeric_limits. A usage example of your class would also be much appreciated.
    $endgroup$
    – Zeta
    15 hours ago










  • $begingroup$
    Some indentation would be nice.
    $endgroup$
    – Quuxplusone
    14 hours ago










  • $begingroup$
    Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing.
    $endgroup$
    – Neargye
    13 hours ago














  • 2




    $begingroup$
    Welcome to Code Review. Could you add some additional information to your code, e.g. if you tried to reproduce the string_view interface, what you want reviewers to focus on, and so on? Also, you want to add missing #includes for std::string and std::numeric_limits. A usage example of your class would also be much appreciated.
    $endgroup$
    – Zeta
    15 hours ago










  • $begingroup$
    Some indentation would be nice.
    $endgroup$
    – Quuxplusone
    14 hours ago










  • $begingroup$
    Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing.
    $endgroup$
    – Neargye
    13 hours ago








2




2




$begingroup$
Welcome to Code Review. Could you add some additional information to your code, e.g. if you tried to reproduce the string_view interface, what you want reviewers to focus on, and so on? Also, you want to add missing #includes for std::string and std::numeric_limits. A usage example of your class would also be much appreciated.
$endgroup$
– Zeta
15 hours ago




$begingroup$
Welcome to Code Review. Could you add some additional information to your code, e.g. if you tried to reproduce the string_view interface, what you want reviewers to focus on, and so on? Also, you want to add missing #includes for std::string and std::numeric_limits. A usage example of your class would also be much appreciated.
$endgroup$
– Zeta
15 hours ago












$begingroup$
Some indentation would be nice.
$endgroup$
– Quuxplusone
14 hours ago




$begingroup$
Some indentation would be nice.
$endgroup$
– Quuxplusone
14 hours ago












$begingroup$
Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing.
$endgroup$
– Neargye
13 hours ago




$begingroup$
Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing.
$endgroup$
– Neargye
13 hours ago










1 Answer
1






active

oldest

votes


















3












$begingroup$


  1. I would only ever mark polymorphic classes final. For them, there's a potential benefit to balance out the unnaturalness of not being able to inherit.


  2. You should really indent the contents of the class by one step. Though that could possibly be an artifact of only partially adapting to SE's markup.


  3. Consider verifying that prefix and suffix have sensible values, at least in debug-mode (assert()).


  4. The first ctor has sensible defaults for its last two parameters. Why not take advantage of that?


  5. Is there a reason you only accept std::string, instead of generally std::basic_string<char, std::char_traits<char>, AnyAllocator>?


  6. You could use !_size instead of _size == 0. Well, de gustibus.


  7. Your comparison is curious, but at least consistent.


  8. I would suggest conforming to C++17 std::string_view as closely as possible so you can later remove your custom class, and to ease the burden for users.







share|improve this answer









$endgroup$













  • $begingroup$
    1. I will consider. The final is a habit from Java. 2. Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing. 3. I'll add a check. 4. Do you mean to make the constructor like this? cstring() : cstring{nullptr, 0} {} 5. Yes, i think std::basic_string<char, std::char_traits<char>, AnyAllocator> will be better. 6. Ok, will be noted. 7. Oh, i forgot add detail::StrCompare to src. Will be fix soon. 8. I'll think about whether I can upgrade the project to C++17
    $endgroup$
    – Neargye
    13 hours ago












  • $begingroup$
    re 4: Yes, that's it.
    $endgroup$
    – Deduplicator
    13 hours ago











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Neargye is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215267%2fstdstring-like-compile-time-const-char-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3












$begingroup$


  1. I would only ever mark polymorphic classes final. For them, there's a potential benefit to balance out the unnaturalness of not being able to inherit.


  2. You should really indent the contents of the class by one step. Though that could possibly be an artifact of only partially adapting to SE's markup.


  3. Consider verifying that prefix and suffix have sensible values, at least in debug-mode (assert()).


  4. The first ctor has sensible defaults for its last two parameters. Why not take advantage of that?


  5. Is there a reason you only accept std::string, instead of generally std::basic_string<char, std::char_traits<char>, AnyAllocator>?


  6. You could use !_size instead of _size == 0. Well, de gustibus.


  7. Your comparison is curious, but at least consistent.


  8. I would suggest conforming to C++17 std::string_view as closely as possible so you can later remove your custom class, and to ease the burden for users.







share|improve this answer









$endgroup$













  • $begingroup$
    1. I will consider. The final is a habit from Java. 2. Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing. 3. I'll add a check. 4. Do you mean to make the constructor like this? cstring() : cstring{nullptr, 0} {} 5. Yes, i think std::basic_string<char, std::char_traits<char>, AnyAllocator> will be better. 6. Ok, will be noted. 7. Oh, i forgot add detail::StrCompare to src. Will be fix soon. 8. I'll think about whether I can upgrade the project to C++17
    $endgroup$
    – Neargye
    13 hours ago












  • $begingroup$
    re 4: Yes, that's it.
    $endgroup$
    – Deduplicator
    13 hours ago
















3












$begingroup$


  1. I would only ever mark polymorphic classes final. For them, there's a potential benefit to balance out the unnaturalness of not being able to inherit.


  2. You should really indent the contents of the class by one step. Though that could possibly be an artifact of only partially adapting to SE's markup.


  3. Consider verifying that prefix and suffix have sensible values, at least in debug-mode (assert()).


  4. The first ctor has sensible defaults for its last two parameters. Why not take advantage of that?


  5. Is there a reason you only accept std::string, instead of generally std::basic_string<char, std::char_traits<char>, AnyAllocator>?


  6. You could use !_size instead of _size == 0. Well, de gustibus.


  7. Your comparison is curious, but at least consistent.


  8. I would suggest conforming to C++17 std::string_view as closely as possible so you can later remove your custom class, and to ease the burden for users.







share|improve this answer









$endgroup$













  • $begingroup$
    1. I will consider. The final is a habit from Java. 2. Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing. 3. I'll add a check. 4. Do you mean to make the constructor like this? cstring() : cstring{nullptr, 0} {} 5. Yes, i think std::basic_string<char, std::char_traits<char>, AnyAllocator> will be better. 6. Ok, will be noted. 7. Oh, i forgot add detail::StrCompare to src. Will be fix soon. 8. I'll think about whether I can upgrade the project to C++17
    $endgroup$
    – Neargye
    13 hours ago












  • $begingroup$
    re 4: Yes, that's it.
    $endgroup$
    – Deduplicator
    13 hours ago














3












3








3





$begingroup$


  1. I would only ever mark polymorphic classes final. For them, there's a potential benefit to balance out the unnaturalness of not being able to inherit.


  2. You should really indent the contents of the class by one step. Though that could possibly be an artifact of only partially adapting to SE's markup.


  3. Consider verifying that prefix and suffix have sensible values, at least in debug-mode (assert()).


  4. The first ctor has sensible defaults for its last two parameters. Why not take advantage of that?


  5. Is there a reason you only accept std::string, instead of generally std::basic_string<char, std::char_traits<char>, AnyAllocator>?


  6. You could use !_size instead of _size == 0. Well, de gustibus.


  7. Your comparison is curious, but at least consistent.


  8. I would suggest conforming to C++17 std::string_view as closely as possible so you can later remove your custom class, and to ease the burden for users.







share|improve this answer









$endgroup$




  1. I would only ever mark polymorphic classes final. For them, there's a potential benefit to balance out the unnaturalness of not being able to inherit.


  2. You should really indent the contents of the class by one step. Though that could possibly be an artifact of only partially adapting to SE's markup.


  3. Consider verifying that prefix and suffix have sensible values, at least in debug-mode (assert()).


  4. The first ctor has sensible defaults for its last two parameters. Why not take advantage of that?


  5. Is there a reason you only accept std::string, instead of generally std::basic_string<char, std::char_traits<char>, AnyAllocator>?


  6. You could use !_size instead of _size == 0. Well, de gustibus.


  7. Your comparison is curious, but at least consistent.


  8. I would suggest conforming to C++17 std::string_view as closely as possible so you can later remove your custom class, and to ease the burden for users.








share|improve this answer












share|improve this answer



share|improve this answer










answered 14 hours ago









DeduplicatorDeduplicator

11.6k1850




11.6k1850












  • $begingroup$
    1. I will consider. The final is a habit from Java. 2. Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing. 3. I'll add a check. 4. Do you mean to make the constructor like this? cstring() : cstring{nullptr, 0} {} 5. Yes, i think std::basic_string<char, std::char_traits<char>, AnyAllocator> will be better. 6. Ok, will be noted. 7. Oh, i forgot add detail::StrCompare to src. Will be fix soon. 8. I'll think about whether I can upgrade the project to C++17
    $endgroup$
    – Neargye
    13 hours ago












  • $begingroup$
    re 4: Yes, that's it.
    $endgroup$
    – Deduplicator
    13 hours ago


















  • $begingroup$
    1. I will consider. The final is a habit from Java. 2. Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing. 3. I'll add a check. 4. Do you mean to make the constructor like this? cstring() : cstring{nullptr, 0} {} 5. Yes, i think std::basic_string<char, std::char_traits<char>, AnyAllocator> will be better. 6. Ok, will be noted. 7. Oh, i forgot add detail::StrCompare to src. Will be fix soon. 8. I'll think about whether I can upgrade the project to C++17
    $endgroup$
    – Neargye
    13 hours ago












  • $begingroup$
    re 4: Yes, that's it.
    $endgroup$
    – Deduplicator
    13 hours ago
















$begingroup$
1. I will consider. The final is a habit from Java. 2. Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing. 3. I'll add a check. 4. Do you mean to make the constructor like this? cstring() : cstring{nullptr, 0} {} 5. Yes, i think std::basic_string<char, std::char_traits<char>, AnyAllocator> will be better. 6. Ok, will be noted. 7. Oh, i forgot add detail::StrCompare to src. Will be fix soon. 8. I'll think about whether I can upgrade the project to C++17
$endgroup$
– Neargye
13 hours ago






$begingroup$
1. I will consider. The final is a habit from Java. 2. Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing. 3. I'll add a check. 4. Do you mean to make the constructor like this? cstring() : cstring{nullptr, 0} {} 5. Yes, i think std::basic_string<char, std::char_traits<char>, AnyAllocator> will be better. 6. Ok, will be noted. 7. Oh, i forgot add detail::StrCompare to src. Will be fix soon. 8. I'll think about whether I can upgrade the project to C++17
$endgroup$
– Neargye
13 hours ago














$begingroup$
re 4: Yes, that's it.
$endgroup$
– Deduplicator
13 hours ago




$begingroup$
re 4: Yes, that's it.
$endgroup$
– Deduplicator
13 hours ago










Neargye is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Neargye is a new contributor. Be nice, and check out our Code of Conduct.













Neargye is a new contributor. Be nice, and check out our Code of Conduct.












Neargye is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215267%2fstdstring-like-compile-time-const-char-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to label and detect the document text images

Vallis Paradisi

Tabula Rosettana