Default constructor defined with default arguments outside the class definition, why does this work? and what happens with templates involved?

寵の児 提交于 2020-01-14 09:00:32

问题


I am aware this is bad form and that default-values should be specified in the declaration, but if you would please indulge me for a moment.. why does this compile? and what is happening exactly?

#include <iostream>
using namespace std;

class test
{
public:
    test(int n);
};

test::test(int n = 666)
{
    cout << n;
}

int main()
{
    test t;

    cin.sync();
    cin.ignore();

    return 0;
}

Output: 666

.. how do templates affect the same piece of code?

template <class T>
class test
{
public:
    test(int n);
};

template <class T>
test<T>::test(int n = 666)
{
    cout << n;
}

int main()
{
    test<int> t;

    cin.sync();
    cin.ignore();

    return 0;
}

Error: no appropriate default constructor available

Thank you for your time!


回答1:


It looks like the C++ specification specifically allows the first case and disallows the second!

Quote from the C++ spec (§8.3.6/4):

For non-template functions, default arguments can be added in later declarations of a function in the same scope.

So it looks like for non-template functions, you can indeed introduce the default arguments later on. No idea why this doesn't work for templates, though!




回答2:


The first case is allowed by standard; I remember that was asked by @Johannes and answered by @Nawaz. (Edit: Here is the related question).

Reason for not allowing the template version is because template functions are called only when instantiated explicitly. In your case, compiler looks at the declaration as,

test<int> t;

-->Edit: It might differ from compiler to compiler. In gcc it works fine.<--

Why it may not work in some compiler might be Since you are not explicitly instantiating as t(N), compiler will not be able to resolve test<T>::test(int n = 666). Thus it looks for default constructor with no argument, which is not found; thus resulting in error.



来源:https://stackoverflow.com/questions/6805507/default-constructor-defined-with-default-arguments-outside-the-class-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!