'Definition of struct template value in constructor / member function
I'm working with a Pascal library that uses UCSD Strings. I created this template struct for making working with them easier:
template <std::size_t N>
struct DString {
unsigned Reference, Size = N;
char String[N];
};
#define MAKE_STRING(X) DString<sizeof(X)>{ 0x0FFFFFFFF, sizeof(X) - 1, X}
auto foo = MAKE_STRING("Foo");
I know it does not cover WideChar cases but the library does neither in the first place so I'm safe in that regard.
Anyway, my problem is that I don't want to use a macro and instead would like to create a constructor. So I was wondering if does C++ offers a possibility of implementing something like this pseudocode:
template <std::size_t N>
struct DString {
unsigned Reference, Size = N;
char String[N];
DString<sizeof(s)>(const char* s, unsigned r = 0x0FFFFFFFF):
String(s), Reference(r) {}
};
auto foo = DString("Foo");
Of course, it does not need to be a "constructor". It is just an example.
I also tried this:
template <std::size_t N>
struct DString {
unsigned Reference, Size = N;
char String[N];
inline static DString build(const char* X) {
return DString<sizeof(X)>{ 0x0FFFFFFFF, sizeof(X) - 1, X};
}
};
auto foo = DString::build("Foo");
But that in itself represents another issue. Because now I cannot reference the static function from DString without doing DString< size >::build(...);.
What can I do in this case?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
