Questions tagged [user-defined-literals]

User-defined literals are a C++ language feature (new in C++11) that allow the user to define new kinds of literal modifiers that will construct objects based on the string of characters that the literal modifies.

User-defined literals are a C++ language feature (new in C++11) that allow the user to define new kinds of literal modifiers that will construct objects based on the string of characters that the literal modifies.

Wikipedia entry: C++11 — User-defined literals

165 questions
0
votes
1 answer

Are there user-defined-literals for variables?

The user-defined-literals from C++11 helps the integers and float numbers have certain units like m, cm, mm, km, and so on. But it seems that it deals only with constant. I have a runtime program which always calculates some lengths and convert them…
Summer Fang
  • 286
  • 3
  • 14
0
votes
1 answer

C++ user-defined literals for any type

For now, user-defined literals accept a limited set of types as input parameter (see here). Is there any plan to accept any type as input parameter, and if not why is that ? For example, I might want to be able to get a std::chrono::duration in…
cmourglia
  • 2,423
  • 1
  • 17
  • 33
0
votes
0 answers

C++11 - User defined string operator overload from char[]

Read about the user defined literals, and find it somehow obscure about defining a string literal overload from char[]. The definitely correct way is like this std::string operator "" _s (const char* m, std::size_t) { return…
neuront
  • 9,312
  • 5
  • 42
  • 71
0
votes
0 answers

Convert char[] into Templated List

Recently I have stumbled across user defined literals, especially the templated variadic char array kinds: template constexpr size_t operator""_size(){ return sizeof...(chars); } //Later constexpr size_t size = "this is…
DarthRubik
  • 3,927
  • 1
  • 18
  • 54
0
votes
2 answers

are C++11 user defined literals slower than normal type casting?

1) is any of these faster than the other at runtime? which and why? 2) does this occur at compile time or runtime? unsigned short operator"" _ushort( unsigned long long arg ){ return arg; } unsigned short my_var = 0x1234; // using type and…
Anon
  • 155
  • 6
0
votes
0 answers

Would it be possible to extend user-defined literals to compounds?

Would it be possible to extend the literal operator to return compounds? Example: typedef struct { double a,b; } Pair; Pair p = (Pair){1.2, 3.4}; // <-- Today: compound literal /* Hypothetical future Operator: */ Pair operator"" _P(double a, double…
ManuelAtWork
  • 2,198
  • 29
  • 34
0
votes
1 answer

Visual Studio 2013 User Defined Literals - Latest CTP

Microsoft is previewing a new CTP for Visual Studio 2013. Click here . How to check if it will have support for C++11 User Defined Literals? I know that previous versions of VS 2013, including the November CTP did not support UDLs, and VS 2015 will…
0
votes
1 answer

Is it possible to implement derived units of SI system using user-defined literals?

I would like to overload user defined literals so that it will allow to perform some physical calculations, e.g auto force = 5_N; // force in newton auto distance = 6.8_m; // distance in meters auto resultingEnergy = force * distance; …
tommyk
  • 3,187
  • 7
  • 39
  • 61
0
votes
1 answer

How do I require a type go through a user defined literal?

I have a POD-type Foo that I want to require users instantiate through my user defined literal (default copying, moving, and assignment are OK): struct Foo { private: Foo() = delete; friend constexpr Foo operator"" _foo (const char* str,…
fbrereto
  • 35,429
  • 19
  • 126
  • 178
0
votes
0 answers

binary literal type conversion

I wrote the following code: g++ -x c++ - -std=gnu++11 -Wall -Wextra -Werror -Wconversion <<__EOF && ./a && echo -e "\e[1;31mOK\e[0m" || echo -e "\e[1;31mfailed!\e[0m" #include #include #include template< typename T…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
0
votes
2 answers

Why doesn't std::string define multiplication or literals?

In the language I was first introduced to, there was a function repeat(), that took a string, and repeated it n times. For example, repeat ("hi", 3) gives a result of "hihihi". I did have quite a few times that I used this function, but to my…
chris
  • 60,560
  • 13
  • 143
  • 205
0
votes
1 answer

Extending class dynamically in Extjs

I need to extend class dynamically and use this code: Calc.grid.Table["Table"+key] = function(config) { config = config || {}; Ext.applyIf(config,{ id: 'calc-grid-table'+key ,baseParams: { …
artask
  • 429
  • 7
  • 18
-1
votes
2 answers

Can a string literal be passed to a function that takes const char*?

I need help understanding some code. I have read in other places that passing a string literal as a const char* is legal. But, in the last line of this code from cppreference for user-defined string literals, it says that there is no literal…
-1
votes
1 answer

How to compose and form the binary literals, for example through the conversion from decimal in C++11 / C++14?

int main() { int x = 3613; std::cout << "x= " << x << std::endl; std::string xBin = std::bitset<16>(x).to_string(); std::cout << xBin << std::endl; unsigned long xDecimal = std::bitset<16>(xBin).to_ulong(); std::cout << xDecimal <<…
user2315094
  • 759
  • 3
  • 16
  • 29
-2
votes
1 answer

User-defined literals

In "User-defined literals" on cppreference.com, what does it mean by this? b) otherwise, the overload set must include either, but not both, a raw literal operator or a numeric literal operator template. If the overload set includes a raw literal…
1 2 3
10
11