使用std :: experimental :: Optional会产生编译错误

时间:2018-07-02 11:57:01

标签: c++ c++11 boost-optional

我是第一次使用可选功能,不管这个代码有什么问题,我希望从存储在retCode中的元组中检索值

#include <iostream>
#include <experimental/optional>
std::experimental::optional<std::tuple<uint16_t, uint32_t, uint32_t>> addEntity();

  std::experimental::optional<std::tuple<uint16_t, uint32_t, uint32_t>> addEntity() {
    uint32_t ipR = 1111;
    uint32_t ipU = 2222;
    uint16_t entityId = 0;
    return std::make_tuple(entityId, ipR, ipU);
  }


int main()
{
  auto retCode =  addEntity();
  std::cout<<std::get<0>(retCode)<<std::endl;
  return 0;

}

编译错误

g++ experiment.cpp
In file included from /usr/include/c++/7/bits/move.h:54:0,
                 from /usr/include/c++/7/bits/nested_exception.h:40,
                 from /usr/include/c++/7/exception:143,
                 from /usr/include/c++/7/ios:39,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from experiment.cpp:1:
/usr/include/c++/7/type_traits: In instantiation of ‘struct std::is_trivially_destructible<std::tuple<short unsigned int, unsigned int, unsigned int> >’:
/usr/include/c++/7/experimental/optional:203:5:   required from ‘class std::experimental::fundamentals_v1::optional<std::tuple<short unsigned int, unsigned int, unsigned int> >’

1 个答案:

答案 0 :(得分:0)

没有从import * as React from 'react'; import styles from './ListItems.module.scss'; import { IListItemsProps } from './IListItemsProps'; import { escape } from '@microsoft/sp-lodash-subset'; import {DropdownBasicExample} from './DropdownExample' import { IDropdownOption, DropdownMenuItemType } from 'office-ui-fabric-react'; import { DropdownBasicExample2 } from './Dropdown2.Basic.Example'; export interface IListItemsState{ selectedItems:IDropdownOption[]; selectedSite:IDropdownOption; } export default class ListItems extends React.Component<IListItemsProps, IListItemsState> { private myWeb:IDropdownOption; constructor(props){ super(props); this.state = { selectedItems:[], selectedSite:null } } private sites = [ { key: 'Header', text: 'Actions', itemType: DropdownMenuItemType.Header }, { key: 'A', text: 'Site a', title: 'I am Site a.' }, { key: 'B', text: 'Site b' }, { key: 'C', text: 'Site c' }, { key: 'D', text: 'Site d' }, { key: 'E', text: 'Site e' }, { key: 'F', text: 'Site f' }, { key: 'G', text: 'Site g' }, { key: 'H', text: 'Site h' }, { key: 'I', text: 'Site i' }, { key: 'J', text: 'Site j' } ]; private loadSites= (): Promise<IDropdownOption[]> => { return new Promise<IDropdownOption[]>((resolve: (sites: IDropdownOption[]) => void, reject: (error: any) => void) => { setTimeout(() => { resolve(this.sites); }, 1000); }); } private onChanged = (item: IDropdownOption, index?: number): void => { let mySelectedItems = [...this.state.selectedItems]; if (item.selected) { mySelectedItems.push(item); } else { mySelectedItems = mySelectedItems.filter(selectedItem => selectedItem !== item); } this.setState({ selectedItems: mySelectedItems }); console.log(mySelectedItems); } public render(): React.ReactElement<IListItemsProps> { const {selectedSite} = this.state; return ( <div className={styles.listItems}> <DropdownBasicExample loadOptions={this.loadSites} onChanged={this.onChanged} /> <div id="showItems"></div> <ul>{ this.state.selectedItems.map((site:IDropdownOption)=> { return <li>{site.text}</li> }) } </ul> <div>selected Site { selectedSite ? selectedSite.key: "is empty" }</div> </div> ); } } import * as React from 'react'; import { PrimaryButton } from 'office-ui-fabric-react/lib/Button'; import { Dropdown, IDropdown, DropdownMenuItemType, IDropdownOption } from 'office-ui-fabric-react/lib/Dropdown'; import './Dropdown.Basic.Example.scss'; import { BaseComponent, createRef, IBaseProps } from 'office-ui-fabric-react'; export interface IDropdownBasicExample2State{ selectedItem?: IDropdownOption; selectedItems: IDropdownOption[]; options: IDropdownOption[]; } export interface IDropdownBasicExample2Props extends IBaseProps{ onChanged?: (option: IDropdownOption, index?: number) => void; Options: IDropdownOption[]; } export class DropdownBasicExample2 extends BaseComponent<IDropdownBasicExample2Props,IDropdownBasicExample2State> { private _basicDropdown = createRef<IDropdown>(); private alphas:IDropdownOption[]; private array2: Array<IDropdownOption>; constructor(props: IDropdownBasicExample2Props) { super(props); this.state = { selectedItem: null, selectedItems: [], options:[] }; } componentDidMount(){ } public render() { const { selectedItem, selectedItems } = this.state; return ( <div className="docs-DropdownExample"> <Dropdown placeHolder="Select options" label="Multi-Select controlled example:" selectedKey={selectedItem ? selectedItem.key : undefined} key={selectedItem ? selectedItem.key : undefined} onChanged={this.onChangeMultiSelect} multiSelect options={this.props.Options} /> </div> ); } public onChangeMultiSelect = (item: IDropdownOption,index:number): void => { this.props.onChanged(item,index); }; } 的隐式转换,您需要提取值。

optional<T>

除非您有其他需要要求 T返回一个可选元组的地方,而不仅仅是您调用int main() { auto retCode = addEntity(); std::cout<<std::get<0>(retCode.value())<<std::endl; // can throw std::bad_optional_access return 0; } 以初始化一个可选地方的地方元组,它应该只是返回元组