2017-04-26 18 views
2

깊은 조사를하고 대신 도움을받을 시간이 없으므로 사과드립니다. C++ 14 + 기본 값을 사용하는 인수에 대한 자동 유형 공제

는 간단한 코드를 고려해

#include <iostream> 

enum class PrintColour 
{ 
    COLOUR_1  = 0, 
    COLOUR_2  = 1, 
}; 

void colour(auto c = PrintColour::COLOUR_1) 
{ 
    switch (c) 
    { 
     case PrintColour::COLOUR_1: 
      std::cout << "Colour 1" << std::endl; 
      break; 
     case PrintColour::COLOUR_2: 
      std::cout << "Colour 2" << std::endl; 
    } 
} 

int main() 
{ 
// colour(); couldn't deduce template parameter ‘auto:1’ 
    colour(PrintColour::COLOUR_1); // Fine! 
} 

가 컴파일하고 문제없이 실행 정확하게이 코드.

auto_param.cpp: In function ‘int main()’: 
auto_param.cpp:27:10: error: no matching function for call to ‘colour()’ 
    colour(); 
     ^
auto_param.cpp:13:6: note: candidate: template<class auto:1> void colour(auto:1) 
void colour(auto c = PrintColour::COLOUR_1) 
     ^~~~~~ 
auto_param.cpp:13:6: note: template argument deduction/substitution failed: 
auto_param.cpp:27:10: note: couldn't deduce template parameter ‘auto:1’ 
    colour(); 
     ^

그냥 바보 같은 점을 잃었거나 정말 바보 오전 전체를 오해 가능성이 가능성이 : 나는 주석을 해제하면 colour();는하지만, g ++는 오류를 발생합니다.

함수 매개 변수를 auto으로 선언해도 C++ 11 또는 C++ 14에서 기본값을 제공 할 수 있어야합니까? 나는 주어진 디폴트 값이 컴파일러로 하여금 매개 변수 유형을 추론 할 수있을 것이라고 생각했다. ...

미리 감사드립니다.


편집 1 :

그것은 여기 Is there a way to pass auto as an argument in C++?

점으로 착각하지 않도록 내가 함수에 auto를 전달하지만, 필요하지 않습니다 내 질문에 명확하게 할 필요가 있다고 생각 auto디폴트 값은이며, 앞서 언급 한 질문에서 고려되지 않은 것입니다.

편집 2 :

C++ (11)가 같은 매개 변수로 auto를 전달하는 기능을하지만, C++ (14) 및 (g ++ 6.3.1 기본값이없는, 여기 의견에 명확히으로 "GNU ++ 14 ")로 보입니다. 내 원래의 질문은 C++ 11과 관련이 없지만 C++ 11이 auto 매개 변수를 지원하는지 여부는 제 질문과 다릅니다. 매개 변수로 auto을 사용했지만 최소한의 표준 버전을 두 번 확인하는 것을 잊었습니다. 내 사과와 나는 이제 그것을 고쳤다.

g++ -std=c++11 auto_param.cpp -o auto_param 
auto_param.cpp:13:14: error: use of ‘auto’ in parameter declaration only available with -std=c++14 or -std=gnu++14 

나는 내 질문과 Is auto as a parameter in a regular function a GCC 4.9 extension?의 차이점을 분명히 알기를 바랍니다. 그렇지 않으면 말해주세요.

+0

의 사용 가능한 복제가 [C++에서 인수로 자동 전달하는 방법이 있나요? (http://stackoverflow.com/questions/29944985/is-there-a-way-to- pass-auto-as-an-argument-in-c) –

+2

* 여기서 '자동'이라고 가정하면 템플릿 매개 변수에 대한 구문 적 설탕 일 뿐이므로 매개 변수 유형을 추론 할 수 없으므로이 함수는 매개 변수를 사용하여 호출 할 수 없습니다 문맥. –

+0

[GCC 4.9 확장 기능에서 자동으로 매개 변수로 사용할 수 있습니까?] (http://stackoverflow.com/questions/25879705/is-auto-as-a-parameter-in-a-regular-function -a-gcc-4-9-extension) – cpplearner

답변

2

Should I be able to declare a function parameter as auto while still being able to give it a default value in C++11 or C++14?

C++ 17 지원은하지만, 내가 아는까지, C++ 11은 C++ 14 기능에 대한 auto 매개 변수를 지원하지 않을 경우 나도 몰라 (C 대신 auto의 템플릿 유형을 사용하도록 동의하는 경우에만 람다 함수에 대한 ++ 14 지원이)

I thought the given default value would be enough to let compiler deduce the parameter type...

, 당신은 또한 기본 템플릿 유형을 추가해야합니다.내가 아는

template <typename T = decltype(PrintColour::COLOUR_1)> 
void colour(T c = PrintColour::COLOUR_1) 
{ 
    switch (c) 
    { 
     case PrintColour::COLOUR_1: 
      std::cout << "Colour 1" << std::endl; 
      break; 
     case PrintColour::COLOUR_2: 
      std::cout << "Colour 2" << std::endl; 
    } 
} 

을 다음과 같은

뭔가 : 중복됩니다.

- 편집 -

영업 이익은 당신은 내가 매크로 악 증류 알고 ... 그것은 반복되지하려면 ...

I was just wondering if I couldn't make my code more readable by not repeating

더 아마도 읽을 수 없습니다하지만라고하지만, ... 당신은 정말는 당신이 트릭을 원하는 경우 (... 반복도

#define noRepeat(r, n, a, b) \ 
r n (decltype(b) a = b) 

noRepeat(void, colour, c, PrintColour::COLOUR_1) 
{ 
    switch (c) 
    { 
     case PrintColour::COLOUR_1: 
      std::cout << "Colour 1" << std::endl; 
      break; 
     case PrintColour::COLOUR_2: 
      std::cout << "Colour 2" << std::endl; 
    } 
} 

또는를 피하려는 경우 매개 변수 기준)

#define parDef(a, b) decltype(b) a = b 

void colour (parDef(c, PrintColour::COLOUR_1), parDef(d, 5)) 
{ 
    switch (c) 
    { 
     case PrintColour::COLOUR_1: 
      std::cout << "Colour 1" << std::endl; 
      break; 
     case PrintColour::COLOUR_2: 
      std::cout << "Colour 2" << std::endl; 
    } 
} 
+0

Thanks @ max66"을 선언합니다. '(PrintColour c = PrintColour :: COLOUR_1)'을 반복하지 않음으로써 코드를 더 읽기 쉽게 만들 수 없는지 궁금합니다. 나는 그것이 작동하지 않는다는 것을 안다. 그러나 나는 아직도 왜 (그리고 아마도 내 코드를 정리할 다른 방법을 배우는) 배울 싶다. @Ior R.의 코멘트에서 알 수 있듯이, 나는 '자동'을 "통사론의 설탕"으로 생각하고있었습니다. – j4x

+1

@fljx - 알겠습니다 ... 반복하지 않으려면 ... 답변이 개선되었습니다. – max66

+0

당신의 편집은 유혹입니다. 나는'#define \t def_deduce (p, def_value) \t decltype (def_value) p = def_value'과 나중에'void color (def_deduce (c, PrintColour :: COLOUR_1))'와 같은 것을 고려하고있다. , 당신이 말한대로. 매크로는 불명예 스럽다. g ++에 따르면, C++ 14는 매개 변수로'auto'를 지원합니다. – j4x

2

아니요, it is a non-deduced context.

Non-deduced contexts.

In the following cases, the types, templates, and non-type values that are used to compose P do not participate in template argument deduction, but instead use the template arguments that were either deduced elsewhere or explicitly specified. If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.

<...>

4) A template parameter used in the parameter type of a function parameter that has a default argument that is being used in the call for which argument deduction is being done

+0

템플릿 인자 공제에 대해 이야기하고 있습니다. OP는'자동차'에 대한 공제에 대해 묻고있다. –

+0

@R Sahu "자동"매개 변수 (미니 개념 TS에 정의 됨)에 대한 유형 공제가 템플릿 매개 변수와 다른 점은 무엇입니까? –

+0

함수 매개 변수에 대한'auto' 유형은 C++ 11에서 지원되지 않습니다. C++ 14의 람다 함수에서만 지원됩니다. C++ 17에서이를 지원하는 제안이 있습니다. 나는 그 제안을 보지 못했다. 나는 그것이 템플릿 인자 공제와 얼마나 유사한지를 말할 수 없다. C++ 11 또는 C++ 14를 다룰 때 우리가 의미있게 논의 할 수있는 것이 아닙니다. –

관련 문제