Выбор конструктора
От: vi.k  
Дата: 31.08.10 04:26
Оценка:
Есть класс с двумя конструкторами:

1) my_class(const std::wstring &str1, const std::wstring &str2, bool a = true)...
и
2) my_class(const std::wstring &str, bool a = true)...

Создаю:
my_class m(L"aaa", L"bbb");

Компилятор (gcc 4.4.4, Feodora) в этом случае выбирает вариант 2.
Выходит, что для него логичнее преобразовать const wchar_t * к bool, чем к const std::wstring?

Есть какие-то объяснения этому?
Re: Выбор конструктора
От: night beast СССР  
Дата: 31.08.10 05:14
Оценка:
Здравствуйте, vi.k, Вы писали:

VK>Компилятор (gcc 4.4.4, Feodora) в этом случае выбирает вариант 2.

VK>Выходит, что для него логичнее преобразовать const wchar_t * к bool, чем к const std::wstring?

VK>Есть какие-то объяснения этому?


C++ Templates: The Complete Guide
By David Vandevoorde, Nicolai M. Josuttis

Given this first principle, we are left with specifying how well a given argument matches the corresponding parameter of a viable candidate. As a first approximation we can rank the possible matches as follows (from best to worst):

* Perfect match. The parameter has the type of the expression, or it has a type that is a reference to the type of the expression (possibly with added const and/or volatile qualifiers).

* Match with minor adjustments. This includes, for example, the decay of an array variable to a pointer to its first element, or the addition of const to match an argument of type int** to a parameter of type int const* const*.

* Match with promotion. Promotion is a kind of implicit conversion that includes the conversion of small integral types (such as bool, char, short, and sometimes enumerations) to int, unsigned int, long or unsigned long, and the conversion of float to double.

* Match with standard conversions only. This includes any sort of standard conversion (such as int to float) but excludes the implicit call to a conversion operator or a converting constructor.

Match with user-defined conversions. This allows any kind of implicit conversion.

Match with ellipsis. An ellipsis parameter can match almost any type (but non-POD class types result in undefined behavior).

Re[2]: Выбор конструктора
От: vi.k  
Дата: 31.08.10 05:26
Оценка:
Здравствуйте, night beast, Вы писали:

NB>Здравствуйте, vi.k, Вы писали:


VK>>Компилятор (gcc 4.4.4, Feodora) в этом случае выбирает вариант 2.

VK>>Выходит, что для него логичнее преобразовать const wchar_t * к bool, чем к const std::wstring?

VK>>Есть какие-то объяснения этому?


NB>C++ Templates: The Complete Guide

NB>By David Vandevoorde, Nicolai M. Josuttis

NB>

NB>Given this first principle, we are left with specifying how well a given argument matches the corresponding parameter of a viable candidate. As a first approximation we can rank the possible matches as follows (from best to worst):

NB>* Perfect match. The parameter has the type of the expression, or it has a type that is a reference to the type of the expression (possibly with added const and/or volatile qualifiers).

NB>* Match with minor adjustments. This includes, for example, the decay of an array variable to a pointer to its first element, or the addition of const to match an argument of type int** to a parameter of type int const* const*.

NB>* Match with promotion. Promotion is a kind of implicit conversion that includes the conversion of small integral types (such as bool, char, short, and sometimes enumerations) to int, unsigned int, long or unsigned long, and the conversion of float to double.

NB>* Match with standard conversions only. This includes any sort of standard conversion (such as int to float) but excludes the implicit call to a conversion operator or a converting constructor.

NB>Match with user-defined conversions. This allows any kind of implicit conversion.

NB>Match with ellipsis. An ellipsis parameter can match almost any type (but non-POD class types result in undefined behavior).



Какой вариант мой? С моим плохим английским, мне кажется, что оба случая попадают в последнюю группу. Но тогда как он разделяет дальше?
Или же преобразование указателя к bool является standard conversions?
Re[3]: Выбор конструктора
От: night beast СССР  
Дата: 31.08.10 05:30
Оценка:
Здравствуйте, vi.k, Вы писали:

NB>>

NB>>* Match with standard conversions only. This includes any sort of standard conversion (such as int to float) but excludes the implicit call to a conversion operator or a converting constructor.



VK>Какой вариант мой? С моим плохим английским, мне кажется, что оба случая попадают в последнюю группу. Но тогда как он разделяет дальше?


нет.

VK>Или же преобразование указателя к bool является standard conversions?


да. там тоже деление, но в твоем примере это не важно.
Re[2]: Выбор конструктора
От: Masterkent  
Дата: 31.08.10 10:22
Оценка:
night beast:

NB>C++ Templates: The Complete Guide

NB>By David Vandevoorde, Nicolai M. Josuttis

NB>

NB>Given this first principle, we are left with specifying how well a given argument matches the corresponding parameter of a viable candidate. As a first approximation we can rank the possible matches as follows (from best to worst):

NB>* Perfect match. The parameter has the type of the expression, or it has a type that is a reference to the type of the expression (possibly with added const and/or volatile qualifiers).

NB>* Match with minor adjustments. This includes, for example, the decay of an array variable to a pointer to its first element, or the addition of const to match an argument of type int** to a parameter of type int const* const*.

NB>* Match with promotion. Promotion is a kind of implicit conversion that includes the conversion of small integral types (such as bool, char, short, and sometimes enumerations) to int, unsigned int, long or unsigned long, and the conversion of float to double.

NB>* Match with standard conversions only. This includes any sort of standard conversion (such as int to float) but excludes the implicit call to a conversion operator or a converting constructor.

NB>Match with user-defined conversions. This allows any kind of implicit conversion.

NB>Match with ellipsis. An ellipsis parameter can match almost any type (but non-POD class types result in undefined behavior).


Это несколько неверная информация. Во-первых, последовательность преобразований, состоящая из array-to-pointer conversion, не хуже, чем пустая последовательность преобразований (см. C++03 — 13.3.3.1.1/3, 13.3.3.2/3/1/1). В следующем примере

#include <cstddef>

template <class T, std::size_t size>
    void f(T (&)[size]);

int f(char const *) { return 0; }

int main()
{
    int value = f("text");
}

должна быть вывана функция f(char const *), требующая array-to-pointer преобразование, хотя при вызове шаблонной функции преобразование вообще бы не потребовалось.

Во-вторых, упомянутые array-to-pointer conversion, qualification adjustment, integral & floating point promotions относятся к standard conversions, которые у авторов книги почему-то выделены особняком.
Re[3]: Выбор конструктора
От: night beast СССР  
Дата: 31.08.10 10:49
Оценка:
Здравствуйте, Masterkent, Вы писали:

NB>>C++ Templates: The Complete Guide

NB>>By David Vandevoorde, Nicolai M. Josuttis

NB>>Given this first principle, we are left with specifying how well a given argument matches the corresponding parameter of a viable candidate. As a first approximation we can rank the possible matches as follows (from best to worst):


M>Это несколько неверная информация. Во-первых, последовательность преобразований, состоящая из array-to-pointer conversion, не хуже, чем пустая последовательность преобразований (см. C++03 — 13.3.3.1.1/3, 13.3.3.2/3/1/1). В следующем примере


это верно, но к исходному вопросу прямого отношения не имеет.

M>Во-вторых, упомянутые array-to-pointer conversion, qualification adjustment, integral & floating point promotions относятся к standard conversions, которые у авторов книги почему-то выделены особняком.


feedback
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.