[英]How to return null from a function whose return type is map in Visual C++?
[英]Can C++ use local variables inside a function whose type is automatically inferred from the function return type?
我的問題:說我在C ++(或C)中定義一個函數。 or that I can use inside the function definition in order to declare a local variable with type inferred from the return type of the function that I'm defining? 有沒有類似於C ++的或 ,我可以在函數定義中使用它來聲明一個局部變量,其類型是從我定義的函數的返回類型推斷出來的?
示例:C和C ++中的常見編碼模式是
SomeType foo() {
SomeType x;
// ... do something to x ...
return x;
}
instead of typing it in explicitly. 而且我希望推斷出第二個而不是明確地輸入它。 以下不起作用,但我希望我能以這種精神做點什么
SomeType foo() {
decltype(return) x; //<-- infer the function return type, which here is SomeType
// ... do something to x ...
return x;
}
對於簡單的返回類型,這並不是什么大問題,但是當返回類型很復雜時(比如返回類型是一個包含大量模板參數的模板類),不必重復那種類型會很好(並且不易出錯)函數內部的定義。
我也不想改變功能定義來實現這一目標。 to a macro, or perhaps make a template function and a template parameter, really I'm looking to see if in general I can infer a type specifically from the return type of the surrounding function. 因此,雖然在上面的示例中它可以將更改為宏,或者可能使成為模板函數而是模板參數,但我真的想看看我是否可以從返回類型中特別推斷出類型周圍的功能。
也許答案是“不可能”,這是公平的,但我想知道這種或那種方式。
在C ++ 11中:
SomeType foo()
{
decltype(foo()) x;
表達式foo()
與foo
的返回值具有相同的類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.