implicit-conversion

How to take advantage of mkNumericOps in Scala?

不打扰是莪最后的温柔 提交于 2021-01-29 09:51:50
问题 I'm trying to define a new type that can behave essentially like a number (for concreteness, let's say a Double ). I'd like to overload operators on this type and I could do this explicitly, but to avoid repetition, I would like to advantage of the methods in NumericOps, which are defined in terms of the abstract methods in Numeric. My understanding is I should be able to just override the methods in Numeric and get the others for free. Here's the simplest attempt I can think of: class

Why does the C++ modulo operator return 0 for -1 % str.size()?

给你一囗甜甜゛ 提交于 2021-01-27 13:41:36
问题 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(); int x = 1 % 3; int y = i % j; int z = i % s.size(); cout << s.size() << endl; // 3 cout << x << endl; // 1 cout << y << endl; // -1 cout << z << endl; // 0 } Why is z = 0? Does it have to do with casting? 回答1: So, stripping down your code to a minimal example, you're asking why this prints 0 : #include <iostream>

Why is implicit conversion not applied to templated function parameter?

佐手、 提交于 2021-01-27 05:02:51
问题 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<const T>(); } }; void conversionToConstRefWorks (const item<const int> &) { } template <typename T> void butNotWhenTemplated (const item<const T> &) { } int main () { item<int> i; item<const int> ci; // these all compile fine: conversionToConstRefWorks(ci); conversionToConstRefWorks(i); butNotWhenTemplated(ci);

Why is implicit conversion not applied to templated function parameter?

与世无争的帅哥 提交于 2021-01-27 05:00:13
问题 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<const T>(); } }; void conversionToConstRefWorks (const item<const int> &) { } template <typename T> void butNotWhenTemplated (const item<const T> &) { } int main () { item<int> i; item<const int> ci; // these all compile fine: conversionToConstRefWorks(ci); conversionToConstRefWorks(i); butNotWhenTemplated(ci);

Why does it seem that func is the same as &func in C? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2020-12-26 03:50:55
问题 This question already has answers here : How does dereferencing of a function pointer happen? (5 answers) Closed last month . According to the GNU C manual, functions can be called using function pointers like so: func (j); /* (*func) (j); would be equivalent. */ So my reasoning here is: func itself is a pointer to the func(int) function. When you call func(j) , you are implicitly accessing the value of the pointer func (you are moving to the memory location where func is), in the same way as

gcc size_t and sizeof arithmetic conversion to int

梦想的初衷 提交于 2020-11-29 07:19:52
问题 I decided to test compile a project with -Wsign-conversion enabled, to see what warnings would come up, and came across something that doesn't seem right, where gcc behaves differently than clang. Can someone please tell me which is correct? I have a function that takes a size_t param: void func(size_t) {} some other struct struct Test {}; and calling code int i = some_initialiser(); func(sizeof(Test) + static_cast<size_t>(i)); So from my understanding, sizeof returns size_t , and arithmetic

gcc size_t and sizeof arithmetic conversion to int

こ雲淡風輕ζ 提交于 2020-11-29 07:19:12
问题 I decided to test compile a project with -Wsign-conversion enabled, to see what warnings would come up, and came across something that doesn't seem right, where gcc behaves differently than clang. Can someone please tell me which is correct? I have a function that takes a size_t param: void func(size_t) {} some other struct struct Test {}; and calling code int i = some_initialiser(); func(sizeof(Test) + static_cast<size_t>(i)); So from my understanding, sizeof returns size_t , and arithmetic

gcc size_t and sizeof arithmetic conversion to int

这一生的挚爱 提交于 2020-11-29 07:18:22
问题 I decided to test compile a project with -Wsign-conversion enabled, to see what warnings would come up, and came across something that doesn't seem right, where gcc behaves differently than clang. Can someone please tell me which is correct? I have a function that takes a size_t param: void func(size_t) {} some other struct struct Test {}; and calling code int i = some_initialiser(); func(sizeof(Test) + static_cast<size_t>(i)); So from my understanding, sizeof returns size_t , and arithmetic