default-arguments

How do I forward declare a template type that has been forward declared elsewhere with a defaulting

六眼飞鱼酱① 提交于 2019-12-24 03:56:07
问题 So the excellent answer to this question states that you can default template types in a forward declaration, however: You can specify each default template argument only once This is more fully specified in the linked question, but Boost's Property Tree stuff works with the ptree type: typedef basic_ptree<std::string, std::string> ptree; But the basic_ptree class is defined like this: template<class Key, class Data, class KeyCompare> class basic_ptree The only reason the ptree typedef is

Interaction between default arguments and parameter pack (GCC and clang disagree)

蹲街弑〆低调 提交于 2019-12-23 08:53:54
问题 I expect the following code to compile: #include <iostream> template <class Tag = void, class T = int, class... Args> void print(T val = T{}, Args... args) { std::cout << val << ' ' << sizeof...(args) << std::endl; } int main() { print(); print(3.14); print(0, 1, 2); } While it compiles on GCC 5.2 (C++11) despite the unused-but-set-parameter warnings, clang 3.6 (C++11) gives the following error messages: main.cpp:4:33: error: missing default argument on parameter 'args' void print(T val = T{}

Am I using default arguments incorrectly?

强颜欢笑 提交于 2019-12-23 07:29:09
问题 I've just started going through a beginners book of C++. I have some java experience (but having said that, I've never used default arguments in java to be honest) So, as mentioned, my issue is with default arguments.. This is the code snippet I'm using: #include <iostream> using namespace std; //add declaration int add(int a, int b); int main (void) { int number1; cout << "Enter the first value to be summed: "; cin >> number1; cout << "\nThe sum is: " << add(number1) << endl; } int add(int a

Am I using default arguments incorrectly?

≡放荡痞女 提交于 2019-12-23 07:29:05
问题 I've just started going through a beginners book of C++. I have some java experience (but having said that, I've never used default arguments in java to be honest) So, as mentioned, my issue is with default arguments.. This is the code snippet I'm using: #include <iostream> using namespace std; //add declaration int add(int a, int b); int main (void) { int number1; cout << "Enter the first value to be summed: "; cin >> number1; cout << "\nThe sum is: " << add(number1) << endl; } int add(int a

Template parameter default to a later one

99封情书 提交于 2019-12-22 06:30:19
问题 This link doesn't answer my question so I'll ask it here: Basically I want to write a template function template <typename Out, typename In> Out f(In x); Here I always need to specify Out when calling f . I don't want to do it every time, so I basically want template <typename Out = In, typename In> Out f(In x); Which means if I don't specify Out , it will default to In . However, this is not possible in C++11. So my question is, is there any way to achieve the effect: calling f(t) will

Why does using this C++ function twice in one line cause a compile error?

和自甴很熟 提交于 2019-12-21 07:07:20
问题 I ran into some trouble trying to implement a smart equality test macro-type template function in Visual C++ 2010 that had to do with a bug in VS in regard to default arguments of template functions. I fixed it by wrapping the value of the parameter in an extra function, but now I found that I can't use the function twice in one line! Header file: // example.h #pragma once #include <limits> namespace myspace { // Need to define this separately to avoid a Visual Studio bug template<typename T>

Unintuitive behaviour with struct initialization and default arguments

柔情痞子 提交于 2019-12-21 03:22:08
问题 public struct Test { public double Val; public Test(double val = double.NaN) { Val = val; } public bool IsValid { get { return !double.IsNaN(Val); } } } Test myTest = new Test(); bool valid = myTest.IsValid; The above gives valid==true because the constructor with default arg is NOT called and the object is created with the standard default val = 0.0. If the struct is a class the behaviour is valid==false which is what I would expect. I find this difference in behaviour and particularly the

What would be a proper invalid value for a pointer?

旧城冷巷雨未停 提交于 2019-12-20 05:27:08
问题 Suppose I have this code. Your basic "if the caller doesn't provide a value, calculate value" scenario. void fun(const char* ptr = NULL) { if (ptr==NULL) { // calculate what ptr value should be } // now handle ptr normally } and call this with either fun(); // don't know the value yet, let fun work it out or fun(something); // use this value However, as it turns out, ptr can have all kinds of values, including NULL, so I can't use NULL as a signal that the caller doesn't provide ptr. So I'm

Skip some arguments in a C++ function?

核能气质少年 提交于 2019-12-17 16:44:17
问题 I have a C++ function that has 5 arguments, all of which have default values. If I pass in the first three arguments, the program will assign a default value to the last two arguments. Is there any way to pass 3 arguments, and skip one in the middle, giving values to say, the first, second, and fifth arguments? 回答1: Not directly, but you might be able to do something with std::bind: int func(int arg1 = 0, int arg2 = 0, int arg3 = 0); // elsewhere... using std::bind; using std::placeholders::

Assigning generic to rest argument and having optional / default arguments

人盡茶涼 提交于 2019-12-11 17:00:02
问题 I am trying to create this function below that takes one optional argument. It's an object, and the only prop needed is value however, I am looking to pass along any ...rest parameters that area also passed in. const useHook = <T extends { value: string }>({ value: v = '', ...props }: T = { value: '' } as T) => { const defaultValue = React.useMemo(() => v, [props]) const [value, setValue, resetValue] = Hooks.useState(defaultValue) const onChange = React.useCallback((e) => setValue(e.target