implicit-conversion Questions
4
Solved
I'm using an API that accepts void* in certain functions. I frequently accidentally pass the wrong pointer type to the function, and of course it compiles fine, but doesn't work at runtime.
Is ther...
Sarita asked 5/8, 2021 at 21:0
3
Solved
A pointer to non-const data can be implicitly converted to a pointer to const data of the same type:
int *x = NULL;
int const *y = x;
Adding additional const qualifiers to match the additional i...
Mikvah asked 20/2, 2011 at 6:41
4
Solved
Given the following program:
#include <iostream>
#include <string>
using namespace std;
struct GenericType{
operator string(){
return "Hello World";
}
operator int(){
return 111;
...
Deandreadeane asked 24/5, 2012 at 18:16
3
Solved
I'm trying to convert numbers into letters. I'm making an array of divs that either need a number or a number and letter. so 1-3 are just 1-3. but 4-13 need to be a/4, b/5, c6 and so on. is there a...
Remainder asked 2/11, 2012 at 19:43
2
Solved
Consider the following useless code:
struct S{
constexpr operator int() const { return 0; }
constexpr auto operator<=>(S) const { return *this; }
};
static_assert(S{} <= S{});
Clang an...
Tenpin asked 17/3, 2021 at 4:11
5
Solved
The bool data type is commonly represented as 0 (as false) and 1 (as true). However, some say that true values can be represented by a value other than 1. If the later statement is true, then the f...
Jaffna asked 20/5, 2019 at 9:42
1
Solved
Consider the following little program:
#include <vector>
class A {
int a;
int b;
public:
explicit A() = default;
A(int _a, int _b) : a(_a), b(_b) {}
int f(const A& a) { ret...
Barbrabarbuda asked 16/2, 2021 at 16:33
2
Solved
I have a situation where I need overload resolution to prefer an overload with an implicit conversion over a template function with the same name.
Consider the following example:
#include <iostr...
Revolving asked 3/2, 2021 at 23:18
2
Solved
I don't understand the following undefined behaviour from C99 standard:
An adjusted parameter type in a function definition is not an object
type (6.9.1)
From the Standard, parameters of a functi...
Katinka asked 19/1, 2021 at 13:32
1
#include <iostream>
#define FUNC() { std::cout << __PRETTY_FUNCTION__ << "\n"; }
void foo(char const*&& ) FUNC() // A
void foo(char const(&)[4]) FUNC() // B...
Dissipated asked 13/1, 2021 at 15:45
2
Solved
I'm having an issue with some template stuff that I've narrowed down to the following example (C++17):
template <typename T> struct item {
operator item<const T> () const { return item...
Karrykarst asked 22/12, 2020 at 0:0
1
Solved
The piece of code below dereferences a nullptr.
struct Foo {
int *bar;
operator int&() {
return *bar;
}
explicit operator bool() const {
return bar;
}
};
int main() {
Foo f {nullptr};
...
Stempson asked 14/11, 2020 at 21:15
1
Solved
I was playing with implicits but I got some behaviour that I don't understand.
I defined a simple class and its companion object (just to make a quick test on implicits) as follows
class SimpleNumb...
Cockfight asked 2/11, 2020 at 18:26
3
Solved
In c++, I can write a class with a constructor that takes a std::string parameter. This will allow me to construct instances of this class from either std::string or char *, due to implicit convers...
Violante asked 5/6, 2014 at 18:9
3
Solved
It compiles with /permissive but fails with /permissive-. What is not conforming and how to fix it?
Why it's fine in (2) but fails in (4)(3)?
If I remove operator long it also fine.
How to fix it...
Intracutaneous asked 6/9, 2019 at 11:43
3
Solved
could someone explain what happens in this code:
example here: https://ideone.com/1cFb4N
#include <iostream>
using namespace std;
class toto
{
public:
bool b;
toto(bool x)
{
cout<<...
Illegality asked 7/10, 2020 at 21:38
1
Solved
I'm having some trouble with an implicit view. I suspect this is quite trivial and may have some embarassingly easy answer. I have a situation like this, along with (obviously, unsuccessful) attemp...
Drumbeat asked 19/9, 2020 at 2:6
3
Solved
Worth a thousand words:
#include<string>
#include<iostream>
class SayWhat {
public:
SayWhat& operator[](const std::string& s) {
std::cout << s << "\n";...
Vines asked 18/11, 2019 at 6:34
6
Solved
A somewhat little-known feature of C# is the possibility to create implicit or explicit user-defined type conversions.
I have been writing C# code for 6 years now, and I have never used it. S...
Devorahdevore asked 26/8, 2012 at 1:0
0
I thought that if the following compiles:
implicitly[X => Y]
than so will this:
(??? :X) :Y
It turns out I was wrong. Backstory: I toyed with an implementation of type unions:
private[this] va...
Laveralavergne asked 20/7, 2020 at 19:21
4
I know that in Java Integer literals are int by default,so if I write something like this
byte byteValue = 2;
Java auto converts the literal value 2(which is an int by default) to byte.
And the sa...
Lin asked 18/7, 2020 at 7:22
1
Solved
Consider method f which is parameterised by a type constructor F[_] and a proper type A
def f[F[_], A](v: F[A]) = v
Lets try to apply it to new Bar
scala> class Bar
class Bar
scala> def ...
Tele asked 16/6, 2020 at 18:21
1
Solved
Surprisingly, the following code compiles well both in gcc and clang no matter what symbol before function name is used: *, & or nothing. Does standard allow any of them? What is preferred way ...
Piave asked 25/5, 2020 at 16:52
1
Solved
Today I saw a really strange type deduction. Here is the code:
unsigned int y = 15;
int k = 5;
auto t = k - y / 2;
Since k is int, I assumed that type of t should be int too. But to my surprise,...
Ibo asked 3/5, 2020 at 18:38
3
Solved
I'm confused why the following code produces this output:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i = -1;
string s = "abc";
int j = s.size();
...
Derosa asked 19/4, 2020 at 3:52
© 2022 - 2024 — McMap. All rights reserved.