std::string like compile-time const char* stringChecking endianness at compile-timeFunction to convert from const char * to char**Compile Time Constant MapEfficient compile-time directed graphCompile-time printf format checkingCompile-time printf-style format checkingCompile-time wildcard pattern matchingCompile-time plugin systemCompile-time plugin system (2)Compile-time string hash

Is having access to past exams cheating and, if yes, could it be proven just by a good grade?

Space in array system equations

The bar has been raised

Is there an equal sign with wider gap?

Things to avoid when using voltage regulators?

Reverse string, can I make it faster?

Is it possible to have an Abelian group under two different binary operations but the binary operations are not distributive?

Is it true that real estate prices mainly go up?

Should QA ask requirements to developers?

Time travel short story where dinosaur doesn't taste like chicken

How much stiffer are 23c tires over 28c?

Built-In Shelves/Bookcases - IKEA vs Built

Grey hair or white hair

PTIJ: How can I halachically kill a vampire?

Good for you! in Russian

Why is Beresheet doing a only a one-way trip?

Norms on fields

Why does Captain Marvel assume the people on this planet know this?

Adding an additional "order by" column gives me a much worse plan

Set and print content of environment variable in cmd.exe subshell?

Look through the portal of every day

How to pass a string to a command that expects a file?

Making a sword in the stone, in a medieval world without magic

Is there an elementary proof that there are infinitely many primes that are *not* completely split in an abelian extension?



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


Checking endianness at compile-timeFunction to convert from const char * to char**Compile Time Constant MapEfficient compile-time directed graphCompile-time printf format checkingCompile-time printf-style format checkingCompile-time wildcard pattern matchingCompile-time plugin systemCompile-time plugin system (2)Compile-time string hash













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 : cstringstr, N - 1, 0, 0

constexpr cstring() noexcept : cstringnullptr, 0, 0, 0

cstring(const std::string& str) noexcept : cstringstr.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::stringstr_, size_.append(s.str_, s.size_);


friend std::string operator+(cstring lhs, cstring rhs)
return std::stringlhs.str_, lhs.size_ + std::stringrhs.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::stringstr_, 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
    8 hours ago










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










  • $begingroup$
    Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing.
    $endgroup$
    – Neargye
    5 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 : cstringstr, N - 1, 0, 0

constexpr cstring() noexcept : cstringnullptr, 0, 0, 0

cstring(const std::string& str) noexcept : cstringstr.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::stringstr_, size_.append(s.str_, s.size_);


friend std::string operator+(cstring lhs, cstring rhs)
return std::stringlhs.str_, lhs.size_ + std::stringrhs.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::stringstr_, 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
    8 hours ago










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










  • $begingroup$
    Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing.
    $endgroup$
    – Neargye
    5 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 : cstringstr, N - 1, 0, 0

constexpr cstring() noexcept : cstringnullptr, 0, 0, 0

cstring(const std::string& str) noexcept : cstringstr.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::stringstr_, size_.append(s.str_, s.size_);


friend std::string operator+(cstring lhs, cstring rhs)
return std::stringlhs.str_, lhs.size_ + std::stringrhs.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::stringstr_, 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 : cstringstr, N - 1, 0, 0

constexpr cstring() noexcept : cstringnullptr, 0, 0, 0

cstring(const std::string& str) noexcept : cstringstr.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::stringstr_, size_.append(s.str_, s.size_);


friend std::string operator+(cstring lhs, cstring rhs)
return std::stringlhs.str_, lhs.size_ + std::stringrhs.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::stringstr_, 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 8 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
    8 hours ago










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










  • $begingroup$
    Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing.
    $endgroup$
    – Neargye
    5 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
    8 hours ago










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










  • $begingroup$
    Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing.
    $endgroup$
    – Neargye
    5 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
8 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
8 hours ago












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




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












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




$begingroup$
Sorry for the indents, they disappeared during writing answer. Overlooked, correct when editing.
$endgroup$
– Neargye
5 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() : cstringnullptr, 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
    5 hours ago











  • $begingroup$
    re 4: Yes, that's it.
    $endgroup$
    – Deduplicator
    5 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() : cstringnullptr, 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
    5 hours ago











  • $begingroup$
    re 4: Yes, that's it.
    $endgroup$
    – Deduplicator
    5 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() : cstringnullptr, 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
    5 hours ago











  • $begingroup$
    re 4: Yes, that's it.
    $endgroup$
    – Deduplicator
    5 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 6 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() : cstringnullptr, 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
    5 hours ago











  • $begingroup$
    re 4: Yes, that's it.
    $endgroup$
    – Deduplicator
    5 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() : cstringnullptr, 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
    5 hours ago











  • $begingroup$
    re 4: Yes, that's it.
    $endgroup$
    – Deduplicator
    5 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() : cstringnullptr, 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
5 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() : cstringnullptr, 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
5 hours ago













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




$begingroup$
re 4: Yes, that's it.
$endgroup$
– Deduplicator
5 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







-c++, c++11

Popular posts from this blog

Frič See also Navigation menuinternal link

Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox