Run Code
|
Code Wall
|
Users
|
Misc
|
Feedback
|
About
|
Login
|
Theme
auto Keyword Example
#include <iostream> #include <cmath> #include <typeinfo> template<class T, class U> auto add(T t, U u) -> decltype(t + u) // the return type is the type of operator+(T, U) { return t + u; } int main() { bool t = true; auto a = 1 + (int)t; std::cout << "type of a: " << typeid(a).name() << '\n'; auto b = 1.2 + 2.3; std::cout << "type of b: " << typeid(b).name() << '\n'; auto c = add(1, 1.2); std::cout << "type of c: " << typeid(c).name() << '\n'; auto d = true; std::cout << "type of d: " << typeid(d).name() << '\n'; float e = 1.2; auto f = e + (float)1.1; std::cout << "type of f: " << typeid(f).name() << '\n'; }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Move Construction
Dynamically allocated array in unique_ptr with custom deleter.
BinaryGap, C++ - Find longest sequence of zeros in binary representation of an integer.
void pointer
UTF-8 in C++11
general
Variadic Template: Make Index Sequence
Unlike C (even C99/C11), C++ allows initializers in if-conditions, so this compiles.
Optional conversions
Apple is not convertible to itself (clang 3.8.0)
Please log in to post a comment.