C C 函式返回值可以不確定嗎

時間 2021-06-02 18:06:58

1樓:蔣甬杭

圖中這種字串是變數型別的情況,可以用模板。

要麼就用多型。

否則返回型別不確定,就沒有固定介面那要怎麼用這個返回值呢……除非返回型別是那種部分固定部分不固定的結構,固定的部分用於判斷不固定部分的型別。

2樓:開源故事

1.最粗暴的可以考慮將不同型別封裝成乙個大型別,比如bigclass

public:

intdouble

char*

string

等等和乙個識別符號flag

這樣函式返回乙個型別,但如果字串是string就往string那部分記憶體裡填值然後flag記憶體填乙個「string」作為標識

3樓:

用variant吧,下面是個簡易不完全variant實現#include

#include

#include

#include

#include

template

COND

,typename

TrueType

,typename

FalseType

>class

IfThenElseT

;template

TrueType

,typename

FalseType

>class

IfThenElseT

,TrueType

,FalseType

>;template

COND

,typename

TrueType

,typename

FalseType

>using

IfThenElse

=typename

IfThenElseT

,TrueType

,FalseType

>::

Type

;template

...Elements

>class

Typelist

;template

List

>class

FrontT

;template

Head

,typename

...Tail

>class

FrontT

,Tail

...>>

;template

List

>using

Front

=typename

FrontT

>::

Type

;template

List

>class

PopFrontT

;template

Head

,typename

...Tail

>class

PopFrontT

,Tail

...>>

;template

List

>using

PopFront

=typename

PopFrontT

>::

Type

;template

List

>class

IsEmpty

;template

<>class

IsEmpty

<>>;template

List

,typenameT,

unsignedN=

0,bool

Empty

=IsEmpty

>::

value

>struct

FindIndexOf

;template

List

,typenameT,

unsigned

N>struct

FindIndexOf

,N,false

>:public

IfThenElse

is_same_v

>,T>,std

::integral_constant

>,FindIndexOf

>,T,N+1

>>;template

List

,typenameT,

unsigned

N>struct

FindIndexOf

,N,true

>;template

List

>class

LargestTypeT

;template

List

>class

LargestTypeT

;template

<>class

LargestTypeT

<>>;template

List

>using

LargestType

=typename

LargestTypeT

>::

Type

;template

...Types

>class

VariantStorage

void

setDiscriminator

(unsigned

chard)

void

*getRawBuffer

()void

const

*getRawBuffer

()const

template

T>T*

getBufferAs

()template

T>Tconst

*getBufferAs

()const

};template

...Types

>class

Variant

;template

typename

...Types

>class

VariantChoice

Derived

const

&getDerived

()const

protected

:constexpr

static

unsigned

Discriminator

=FindIndexOf

...>,T

>::

value+1

;public

:VariantChoice()=

default

;VariantChoice(T

const

&value

);VariantChoice(T

&&value

);bool

destroy

();Derived

&operator=(

Tconst

&value

);Derived

&operator=(

T&&value

);};

template

typename

...Types

>VariantChoice

...>::

VariantChoice(T

const

&value

)template

typename

...Types

>VariantChoice

...>::

VariantChoice(T

&&value

)template

typename

...Types

>bool

VariantChoice

...>::

destroy

()return

false;}

template

typename

...Types

>auto

VariantChoice

...>::

operator=(

Tconst

&value

)->Derived

&else

return

getDerived

();}

template

typename

...Types

>auto

VariantChoice

...>::

operator=(

T&&value

)->Derived

&else

return

getDerived

();}

class

ComputedResultType

;class

EmptyVariant

:public

std::

exception

;template

...Types

>class

Variant

:private

VariantStorage

...>

,private

VariantChoice

,Types

...>

...private:};

template

...Types

>template

T>bool

Variant

...>::

is()

const

template

...Types

>template

T>T&

Variant

...>::

get()

&assert(is

());

return

*this

->template

getBufferAs

();}

template

...Types

>template

T>T&&

Variant

...>::

get()

&&assert(is

());

return

*this

->template

getBufferAs

();}

template

...Types

>template

T>Tconst

&Variant

...>::

get()

const

&assert(is

());

return

*this

->template

getBufferAs

();}

template

...Types

>void

Variant

...>::

destroy();

(VariantChoice

,Types

...>::

destroy

(),...);

//g++報錯

this

->setDiscriminator(0);}

4樓:普通少年pro max

建議的做法是使用模板,根據不同型別在編譯期例項化對應函式。

C++17的std::optional也可以,返回值型別為optional型別即可。

這個就是封裝後的union,但這個用起來,有一些型別,不注意的話,會啟用錯誤的選項,使用的話要特別注意下,特別對於你要返回布林值和int型別的話,一不小心布林值就把int啟用了,建議參考下StackOverFlow中相關回答,熟練掌握後再應用。

5樓:NoneType

不能。但是你可以使用變數模板:

#include

using

namespace

std;

template

T>Tset

;int

main()

6樓:

如果型別是編譯期確定的,那你更需要的是 C++ 裡的模板。

template

T>template

<>bool

f

>()template

<>intf

()template

<>std::

string

f

string

>()int

main

()如果非得要動態型別不可的話,可以用 variant。

#include

#include

std::

variant

,int

,std

::string

>f(const

std::

string&s

)elseif(

s=="int"

)else

returnv;

}int

main();

autov=

f("bool"

);std

::visit

(visitor,v

);v=f

("int"

);std

::visit

(visitor,v

);v=f

("float"

);std

::visit

(visitor,v

);return0;}

Win API 函式的返回值設計問題?

北極 微軟的使用者API的設計風格大概可以總結為 如果返回的是某種狀態 成功 失敗 那麼TRUE是成功,FALSE是失敗。如果返回的是某種指標 控制代碼 引用 那麼NULL 有些是INVALID HANDLE VALUE 是失敗,非NULL是成功。如果API發生錯誤,錯誤狀態不在API呼叫中顯式返回...

Objective C中函式的返回值是什麼?每個類都有對應的幾種方法嗎?int又是什麼意思呢?

別學習Obj C,這個東西不是人類用的東西。在水果平台,建議學習swift語言吧。這個東西自學真的很累,我就是自學的。另外,有個會程式設計的男朋友真的事半功倍!本人從事變成10年了,哈哈哈哈。 首先要理解類和函式是什麼個東西。函式就是為了操作某些資料。而類是對一些資料的封裝。比如我寫乙個美女類,沒有...

這個函式的返回值為什麼是None?

Seven00007 deftest i ifi 1 return1r test 2 輸出None print r 究其原因,是下面的if條件沒有通過,並沒有走到return這一步程式就返回了,獲取不到返回值,輸出就是None if order info 訂單狀態 完全成交 or order inf...